55 lines
2.3 KiB
JavaScript
55 lines
2.3 KiB
JavaScript
import * as React from 'react';
|
|
const errorRef = {
|
|
current: null
|
|
};
|
|
// React.cache is currently only available in canary/experimental React channels.
|
|
const cache = typeof React.cache === 'function' ? React.cache : (fn)=>fn;
|
|
// When Dynamic IO is enabled, we record these as errors so that they
|
|
// are captured by the dev overlay as it's more critical to fix these
|
|
// when enabled.
|
|
const logErrorOrWarn = process.env.__NEXT_DYNAMIC_IO ? console.error : console.warn;
|
|
// We don't want to dedupe across requests.
|
|
// The developer might've just attempted to fix the warning so we should warn again if it still happens.
|
|
const flushCurrentErrorIfNew = cache(// eslint-disable-next-line @typescript-eslint/no-unused-vars -- cache key
|
|
(key)=>{
|
|
try {
|
|
logErrorOrWarn(errorRef.current);
|
|
} finally{
|
|
errorRef.current = null;
|
|
}
|
|
});
|
|
/**
|
|
* Creates a function that logs an error message that is deduped by the userland
|
|
* callsite.
|
|
* This requires no indirection between the call of this function and the userland
|
|
* callsite i.e. there's only a single library frame above this.
|
|
* Do not use on the Client where sourcemaps and ignore listing might be enabled.
|
|
* Only use that for warnings need a fix independent of the callstack.
|
|
*
|
|
* @param getMessage
|
|
* @returns
|
|
*/ export function createDedupedByCallsiteServerErrorLoggerDev(getMessage) {
|
|
return function logDedupedError(...args) {
|
|
const message = getMessage(...args);
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
var _stack;
|
|
const callStackFrames = (_stack = new Error().stack) == null ? void 0 : _stack.split('\n');
|
|
if (callStackFrames === undefined || callStackFrames.length < 4) {
|
|
logErrorOrWarn(message);
|
|
} else {
|
|
// Error:
|
|
// logDedupedError
|
|
// asyncApiBeingAccessedSynchronously
|
|
// <userland callsite>
|
|
// TODO: This breaks if sourcemaps with ignore lists are enabled.
|
|
const key = callStackFrames[4];
|
|
errorRef.current = message;
|
|
flushCurrentErrorIfNew(key);
|
|
}
|
|
} else {
|
|
logErrorOrWarn(message);
|
|
}
|
|
};
|
|
}
|
|
|
|
//# sourceMappingURL=create-deduped-by-callsite-server-error-logger.js.map
|