inital commit

This commit is contained in:
2026-01-01 15:25:19 +05:30
commit f0ae49465a
36361 changed files with 4894111 additions and 0 deletions

View File

@@ -0,0 +1 @@
export declare function attachHydrationErrorState(error: Error): void;

View File

@@ -0,0 +1,82 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "attachHydrationErrorState", {
enumerable: true,
get: function() {
return attachHydrationErrorState;
}
});
const _ishydrationerror = require("../is-hydration-error");
const _hydrationerrorinfo = require("./hydration-error-info");
function attachHydrationErrorState(error) {
let parsedHydrationErrorState = {};
const isHydrationWarning = (0, _ishydrationerror.testReactHydrationWarning)(error.message);
const isHydrationRuntimeError = (0, _ishydrationerror.isHydrationError)(error);
// If it's not hydration warnings or errors, skip
if (!(isHydrationRuntimeError || isHydrationWarning)) {
return;
}
const reactHydrationDiffSegments = (0, _hydrationerrorinfo.getReactHydrationDiffSegments)(error.message);
// If the reactHydrationDiffSegments exists
// and the diff (reactHydrationDiffSegments[1]) exists
// e.g. the hydration diff log error.
if (reactHydrationDiffSegments) {
const diff = reactHydrationDiffSegments[1];
parsedHydrationErrorState = {
...error.details,
..._hydrationerrorinfo.hydrationErrorState,
// If diff is present in error, we don't need to pick up the console logged warning.
// - if hydration error has diff, and is not hydration diff log, then it's a normal hydration error.
// - if hydration error no diff, then leverage the one from the hydration diff log.
warning: (diff && !isHydrationWarning ? null : _hydrationerrorinfo.hydrationErrorState.warning) || [
(0, _ishydrationerror.getDefaultHydrationErrorMessage)()
],
// When it's hydration diff log, do not show notes section.
// This condition is only for the 1st squashed error.
notes: isHydrationWarning ? '' : reactHydrationDiffSegments[0],
reactOutputComponentDiff: diff
};
// Cache the `reactOutputComponentDiff` into hydrationErrorState.
// This is only required for now when we still squashed the hydration diff log into hydration error.
// Once the all error is logged to dev overlay in order, this will go away.
if (!_hydrationerrorinfo.hydrationErrorState.reactOutputComponentDiff && diff) {
_hydrationerrorinfo.hydrationErrorState.reactOutputComponentDiff = diff;
}
// If it's hydration runtime error that doesn't contain the diff, combine the diff from the cached hydration diff.
if (!diff && isHydrationRuntimeError && _hydrationerrorinfo.hydrationErrorState.reactOutputComponentDiff) {
parsedHydrationErrorState.reactOutputComponentDiff = _hydrationerrorinfo.hydrationErrorState.reactOutputComponentDiff;
}
} else {
// Normal runtime error, where it doesn't contain the hydration diff.
// If there's any extra information in the error message to display,
// append it to the error message details property
if (_hydrationerrorinfo.hydrationErrorState.warning) {
// The patched console.error found hydration errors logged by React
// Append the logged warning to the error message
parsedHydrationErrorState = {
...error.details,
// It contains the warning, component stack, server and client tag names
..._hydrationerrorinfo.hydrationErrorState
};
}
// Consume the cached hydration diff.
// This is only required for now when we still squashed the hydration diff log into hydration error.
// Once the all error is logged to dev overlay in order, this will go away.
if (_hydrationerrorinfo.hydrationErrorState.reactOutputComponentDiff) {
parsedHydrationErrorState.reactOutputComponentDiff = _hydrationerrorinfo.hydrationErrorState.reactOutputComponentDiff;
}
}
// If it's a hydration error, store the hydration error state into the error object
;
error.details = parsedHydrationErrorState;
}
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
Object.defineProperty(exports.default, '__esModule', { value: true });
Object.assign(exports.default, exports);
module.exports = exports.default;
}
//# sourceMappingURL=attach-hydration-error-state.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,11 @@
declare const digestSym: unique symbol;
declare const consoleTypeSym: unique symbol;
type UnhandledError = Error & {
[digestSym]: 'NEXT_UNHANDLED_ERROR';
[consoleTypeSym]: 'string' | 'error';
environmentName: string;
};
export declare function createUnhandledError(message: string | Error, environmentName?: string | null): UnhandledError;
export declare const isUnhandledConsoleOrRejection: (error: any) => error is UnhandledError;
export declare const getUnhandledErrorType: (error: UnhandledError) => "string" | "error";
export {};

View File

@@ -0,0 +1,56 @@
// To distinguish from React error.digest, we use a different symbol here to determine if the error is from console.error or unhandled promise rejection.
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
createUnhandledError: null,
getUnhandledErrorType: null,
isUnhandledConsoleOrRejection: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
createUnhandledError: function() {
return createUnhandledError;
},
getUnhandledErrorType: function() {
return getUnhandledErrorType;
},
isUnhandledConsoleOrRejection: function() {
return isUnhandledConsoleOrRejection;
}
});
const digestSym = Symbol.for('next.console.error.digest');
const consoleTypeSym = Symbol.for('next.console.error.type');
function createUnhandledError(message, environmentName) {
const error = typeof message === 'string' ? Object.defineProperty(new Error(message), "__NEXT_ERROR_CODE", {
value: "E394",
enumerable: false,
configurable: true
}) : message;
error[digestSym] = 'NEXT_UNHANDLED_ERROR';
error[consoleTypeSym] = typeof message === 'string' ? 'string' : 'error';
if (environmentName && !error.environmentName) {
error.environmentName = environmentName;
}
return error;
}
const isUnhandledConsoleOrRejection = (error)=>{
return error && error[digestSym] === 'NEXT_UNHANDLED_ERROR';
};
const getUnhandledErrorType = (error)=>{
return error[consoleTypeSym];
};
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
Object.defineProperty(exports.default, '__esModule', { value: true });
Object.assign(exports.default, exports);
module.exports = exports.default;
}
//# sourceMappingURL=console-error.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/errors/console-error.ts"],"sourcesContent":["// To distinguish from React error.digest, we use a different symbol here to determine if the error is from console.error or unhandled promise rejection.\nconst digestSym = Symbol.for('next.console.error.digest')\nconst consoleTypeSym = Symbol.for('next.console.error.type')\n\n// Represent non Error shape unhandled promise rejections or console.error errors.\n// Those errors will be captured and displayed in Error Overlay.\ntype UnhandledError = Error & {\n [digestSym]: 'NEXT_UNHANDLED_ERROR'\n [consoleTypeSym]: 'string' | 'error'\n environmentName: string\n}\n\nexport function createUnhandledError(\n message: string | Error,\n environmentName?: string | null\n): UnhandledError {\n const error = (\n typeof message === 'string' ? new Error(message) : message\n ) as UnhandledError\n error[digestSym] = 'NEXT_UNHANDLED_ERROR'\n error[consoleTypeSym] = typeof message === 'string' ? 'string' : 'error'\n\n if (environmentName && !error.environmentName) {\n error.environmentName = environmentName\n }\n\n return error\n}\n\nexport const isUnhandledConsoleOrRejection = (\n error: any\n): error is UnhandledError => {\n return error && error[digestSym] === 'NEXT_UNHANDLED_ERROR'\n}\n\nexport const getUnhandledErrorType = (error: UnhandledError) => {\n return error[consoleTypeSym]\n}\n"],"names":["createUnhandledError","getUnhandledErrorType","isUnhandledConsoleOrRejection","digestSym","Symbol","for","consoleTypeSym","message","environmentName","error","Error"],"mappings":"AAAA,yJAAyJ;;;;;;;;;;;;;;;;;IAYzIA,oBAAoB;eAApBA;;IAuBHC,qBAAqB;eAArBA;;IANAC,6BAA6B;eAA7BA;;;AA5Bb,MAAMC,YAAYC,OAAOC,GAAG,CAAC;AAC7B,MAAMC,iBAAiBF,OAAOC,GAAG,CAAC;AAU3B,SAASL,qBACdO,OAAuB,EACvBC,eAA+B;IAE/B,MAAMC,QACJ,OAAOF,YAAY,WAAW,qBAAkB,CAAlB,IAAIG,MAAMH,UAAV,qBAAA;eAAA;oBAAA;sBAAA;IAAiB,KAAIA;IAErDE,KAAK,CAACN,UAAU,GAAG;IACnBM,KAAK,CAACH,eAAe,GAAG,OAAOC,YAAY,WAAW,WAAW;IAEjE,IAAIC,mBAAmB,CAACC,MAAMD,eAAe,EAAE;QAC7CC,MAAMD,eAAe,GAAGA;IAC1B;IAEA,OAAOC;AACT;AAEO,MAAMP,gCAAgC,CAC3CO;IAEA,OAAOA,SAASA,KAAK,CAACN,UAAU,KAAK;AACvC;AAEO,MAAMF,wBAAwB,CAACQ;IACpC,OAAOA,KAAK,CAACH,eAAe;AAC9B"}

View File

@@ -0,0 +1 @@
export declare function enqueueConsecutiveDedupedError(queue: Array<Error>, error: Error): void;

View File

@@ -0,0 +1,27 @@
// Dedupe the two consecutive errors: If the previous one is same as current one, ignore the current one.
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "enqueueConsecutiveDedupedError", {
enumerable: true,
get: function() {
return enqueueConsecutiveDedupedError;
}
});
function enqueueConsecutiveDedupedError(queue, error) {
const previousError = queue[queue.length - 1];
// Compare the error stack to dedupe the consecutive errors
if (previousError && previousError.stack === error.stack) {
return;
}
queue.push(error);
}
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
Object.defineProperty(exports.default, '__esModule', { value: true });
Object.assign(exports.default, exports);
module.exports = exports.default;
}
//# sourceMappingURL=enqueue-client-error.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/errors/enqueue-client-error.ts"],"sourcesContent":["// Dedupe the two consecutive errors: If the previous one is same as current one, ignore the current one.\nexport function enqueueConsecutiveDedupedError(\n queue: Array<Error>,\n error: Error\n) {\n const previousError = queue[queue.length - 1]\n // Compare the error stack to dedupe the consecutive errors\n if (previousError && previousError.stack === error.stack) {\n return\n }\n queue.push(error)\n}\n"],"names":["enqueueConsecutiveDedupedError","queue","error","previousError","length","stack","push"],"mappings":"AAAA,yGAAyG;;;;;+BACzFA;;;eAAAA;;;AAAT,SAASA,+BACdC,KAAmB,EACnBC,KAAY;IAEZ,MAAMC,gBAAgBF,KAAK,CAACA,MAAMG,MAAM,GAAG,EAAE;IAC7C,2DAA2D;IAC3D,IAAID,iBAAiBA,cAAcE,KAAK,KAAKH,MAAMG,KAAK,EAAE;QACxD;IACF;IACAJ,MAAMK,IAAI,CAACJ;AACb"}

View File

@@ -0,0 +1,20 @@
export type HydrationErrorState = {
warning?: [string, string, string];
componentStack?: string;
serverContent?: string;
clientContent?: string;
notes?: string;
reactOutputComponentDiff?: string;
};
type NullableText = string | null | undefined;
export declare const hydrationErrorState: HydrationErrorState;
export declare const getHydrationWarningType: (message: NullableText) => "tag" | "text" | "text-in-tag";
export declare const getReactHydrationDiffSegments: (msg: NullableText) => (string | undefined)[] | undefined;
/**
* Patch console.error to capture hydration errors.
* If any of the knownHydrationWarnings are logged, store the message and component stack.
* When the hydration runtime error is thrown, the message and component stack are added to the error.
* This results in a more helpful error message in the error overlay.
*/
export declare function storeHydrationErrorStateFromConsoleArgs(...args: any[]): void;
export {};

View File

@@ -0,0 +1,171 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
getHydrationWarningType: null,
getReactHydrationDiffSegments: null,
hydrationErrorState: null,
storeHydrationErrorStateFromConsoleArgs: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
getHydrationWarningType: function() {
return getHydrationWarningType;
},
getReactHydrationDiffSegments: function() {
return getReactHydrationDiffSegments;
},
hydrationErrorState: function() {
return hydrationErrorState;
},
storeHydrationErrorStateFromConsoleArgs: function() {
return storeHydrationErrorStateFromConsoleArgs;
}
});
const _ishydrationerror = require("../is-hydration-error");
const hydrationErrorState = {};
// https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js used as a reference
const htmlTagsWarnings = new Set([
'Warning: In HTML, %s cannot be a child of <%s>.%s\nThis will cause a hydration error.%s',
'Warning: In HTML, %s cannot be a descendant of <%s>.\nThis will cause a hydration error.%s',
'Warning: In HTML, text nodes cannot be a child of <%s>.\nThis will cause a hydration error.',
"Warning: In HTML, whitespace text nodes cannot be a child of <%s>. Make sure you don't have any extra whitespace between tags on each line of your source code.\nThis will cause a hydration error.",
'Warning: Expected server HTML to contain a matching <%s> in <%s>.%s',
'Warning: Did not expect server HTML to contain a <%s> in <%s>.%s'
]);
const textAndTagsMismatchWarnings = new Set([
'Warning: Expected server HTML to contain a matching text node for "%s" in <%s>.%s',
'Warning: Did not expect server HTML to contain the text node "%s" in <%s>.%s'
]);
const getHydrationWarningType = (message)=>{
if (typeof message !== 'string') {
// TODO: Doesn't make sense to treat no message as a hydration error message.
// We should bail out somewhere earlier.
return 'text';
}
const normalizedMessage = message.startsWith('Warning: ') ? message : "Warning: " + message;
if (isHtmlTagsWarning(normalizedMessage)) return 'tag';
if (isTextInTagsMismatchWarning(normalizedMessage)) return 'text-in-tag';
return 'text';
};
const isHtmlTagsWarning = (message)=>htmlTagsWarnings.has(message);
const isTextInTagsMismatchWarning = (msg)=>textAndTagsMismatchWarnings.has(msg);
const getReactHydrationDiffSegments = (msg)=>{
if (msg) {
const { message, diff } = (0, _ishydrationerror.getHydrationErrorStackInfo)(msg);
if (message) return [
message,
diff
];
}
return undefined;
};
function storeHydrationErrorStateFromConsoleArgs() {
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
args[_key] = arguments[_key];
}
let [msg, firstContent, secondContent, ...rest] = args;
if ((0, _ishydrationerror.testReactHydrationWarning)(msg)) {
// Some hydration warnings has 4 arguments, some has 3, fallback to the last argument
// when the 3rd argument is not the component stack but an empty string
const isReact18 = msg.startsWith('Warning: ');
// For some warnings, there's only 1 argument for template.
// The second argument is the diff or component stack.
if (args.length === 3) {
secondContent = '';
}
const warning = [
// remove the last %s from the message
msg,
firstContent,
secondContent
];
const lastArg = (rest[rest.length - 1] || '').trim();
if (!isReact18) {
hydrationErrorState.reactOutputComponentDiff = lastArg;
} else {
hydrationErrorState.reactOutputComponentDiff = generateHydrationDiffReact18(msg, firstContent, secondContent, lastArg);
}
hydrationErrorState.warning = warning;
hydrationErrorState.serverContent = firstContent;
hydrationErrorState.clientContent = secondContent;
}
}
/*
* Some hydration errors in React 18 does not have the diff in the error message.
* Instead it has the error stack trace which is component stack that we can leverage.
* Will parse the diff from the error stack trace
* e.g.
* Warning: Expected server HTML to contain a matching <div> in <p>.
* at div
* at p
* at div
* at div
* at Page
* output:
* <Page>
* <div>
* <p>
* > <div>
*
*/ function generateHydrationDiffReact18(message, firstContent, secondContent, lastArg) {
const componentStack = lastArg;
let firstIndex = -1;
let secondIndex = -1;
const hydrationWarningType = getHydrationWarningType(message);
// at div\n at Foo\n at Bar (....)\n -> [div, Foo]
const components = componentStack.split('\n')// .reverse()
.map((line, index)=>{
// `<space>at <component> (<location>)` -> `at <component> (<location>)`
line = line.trim();
// extract `<space>at <component>` to `<<component>>`
// e.g. ` at Foo` -> `<Foo>`
const [, component, location] = /at (\w+)( \((.*)\))?/.exec(line) || [];
// If there's no location then it's user-land stack frame
if (!location) {
if (component === firstContent && firstIndex === -1) {
firstIndex = index;
} else if (component === secondContent && secondIndex === -1) {
secondIndex = index;
}
}
return location ? '' : component;
}).filter(Boolean).reverse();
let diff = '';
for(let i = 0; i < components.length; i++){
const component = components[i];
const matchFirstContent = hydrationWarningType === 'tag' && i === components.length - firstIndex - 1;
const matchSecondContent = hydrationWarningType === 'tag' && i === components.length - secondIndex - 1;
if (matchFirstContent || matchSecondContent) {
const spaces = ' '.repeat(Math.max(i * 2 - 2, 0) + 2);
diff += "> " + spaces + "<" + component + ">\n";
} else {
const spaces = ' '.repeat(i * 2 + 2);
diff += spaces + "<" + component + ">\n";
}
}
if (hydrationWarningType === 'text') {
const spaces = ' '.repeat(components.length * 2);
diff += "+ " + spaces + '"' + firstContent + '"\n';
diff += "- " + spaces + '"' + secondContent + '"\n';
} else if (hydrationWarningType === 'text-in-tag') {
const spaces = ' '.repeat(components.length * 2);
diff += "> " + spaces + "<" + secondContent + ">\n";
diff += "> " + spaces + '"' + firstContent + '"\n';
}
return diff;
}
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
Object.defineProperty(exports.default, '__esModule', { value: true });
Object.assign(exports.default, exports);
module.exports = exports.default;
}
//# sourceMappingURL=hydration-error-info.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,3 @@
export declare const RuntimeErrorHandler: {
hadRuntimeError: boolean;
};

View File

@@ -0,0 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "RuntimeErrorHandler", {
enumerable: true,
get: function() {
return RuntimeErrorHandler;
}
});
const RuntimeErrorHandler = {
hadRuntimeError: false
};
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
Object.defineProperty(exports.default, '__esModule', { value: true });
Object.assign(exports.default, exports);
module.exports = exports.default;
}
//# sourceMappingURL=runtime-error-handler.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/errors/runtime-error-handler.ts"],"sourcesContent":["export const RuntimeErrorHandler = {\n hadRuntimeError: false,\n}\n"],"names":["RuntimeErrorHandler","hadRuntimeError"],"mappings":";;;;+BAAaA;;;eAAAA;;;AAAN,MAAMA,sBAAsB;IACjCC,iBAAiB;AACnB"}

View File

@@ -0,0 +1 @@
export declare function getReactStitchedError<T = unknown>(err: T): Error | T;

View File

@@ -0,0 +1,60 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "getReactStitchedError", {
enumerable: true,
get: function() {
return getReactStitchedError;
}
});
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
const _react = /*#__PURE__*/ _interop_require_default._(require("react"));
const _iserror = /*#__PURE__*/ _interop_require_default._(require("../../../lib/is-error"));
const _errortelemetryutils = require("../../../lib/error-telemetry-utils");
const REACT_ERROR_STACK_BOTTOM_FRAME = 'react-stack-bottom-frame';
const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX = new RegExp("(at " + REACT_ERROR_STACK_BOTTOM_FRAME + " )|(" + REACT_ERROR_STACK_BOTTOM_FRAME + "\\@)");
function getReactStitchedError(err) {
const isErrorInstance = (0, _iserror.default)(err);
const originStack = isErrorInstance ? err.stack || '' : '';
const originMessage = isErrorInstance ? err.message : '';
const stackLines = originStack.split('\n');
const indexOfSplit = stackLines.findIndex((line)=>REACT_ERROR_STACK_BOTTOM_FRAME_REGEX.test(line));
const isOriginalReactError = indexOfSplit >= 0 // has the react-stack-bottom-frame
;
let newStack = isOriginalReactError ? stackLines.slice(0, indexOfSplit).join('\n') : originStack;
const newError = Object.defineProperty(new Error(originMessage), "__NEXT_ERROR_CODE", {
value: "E394",
enumerable: false,
configurable: true
});
// Copy all enumerable properties, e.g. digest
Object.assign(newError, err);
(0, _errortelemetryutils.copyNextErrorCode)(err, newError);
newError.stack = newStack;
// Avoid duplicate overriding stack frames
appendOwnerStack(newError);
return newError;
}
function appendOwnerStack(error) {
if (!_react.default.captureOwnerStack) {
return;
}
let stack = error.stack || '';
// This module is only bundled in development mode so this is safe.
const ownerStack = _react.default.captureOwnerStack();
// Avoid duplicate overriding stack frames
if (ownerStack && stack.endsWith(ownerStack) === false) {
stack += ownerStack;
// Override stack
error.stack = stack;
}
}
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
Object.defineProperty(exports.default, '__esModule', { value: true });
Object.assign(exports.default, exports);
module.exports = exports.default;
}
//# sourceMappingURL=stitched-error.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/client/components/errors/stitched-error.ts"],"sourcesContent":["import React from 'react'\nimport isError from '../../../lib/is-error'\nimport { copyNextErrorCode } from '../../../lib/error-telemetry-utils'\n\nconst REACT_ERROR_STACK_BOTTOM_FRAME = 'react-stack-bottom-frame'\nconst REACT_ERROR_STACK_BOTTOM_FRAME_REGEX = new RegExp(\n `(at ${REACT_ERROR_STACK_BOTTOM_FRAME} )|(${REACT_ERROR_STACK_BOTTOM_FRAME}\\\\@)`\n)\n\nexport function getReactStitchedError<T = unknown>(err: T): Error | T {\n const isErrorInstance = isError(err)\n const originStack = isErrorInstance ? err.stack || '' : ''\n const originMessage = isErrorInstance ? err.message : ''\n const stackLines = originStack.split('\\n')\n const indexOfSplit = stackLines.findIndex((line) =>\n REACT_ERROR_STACK_BOTTOM_FRAME_REGEX.test(line)\n )\n const isOriginalReactError = indexOfSplit >= 0 // has the react-stack-bottom-frame\n let newStack = isOriginalReactError\n ? stackLines.slice(0, indexOfSplit).join('\\n')\n : originStack\n\n const newError = new Error(originMessage)\n // Copy all enumerable properties, e.g. digest\n Object.assign(newError, err)\n copyNextErrorCode(err, newError)\n newError.stack = newStack\n\n // Avoid duplicate overriding stack frames\n appendOwnerStack(newError)\n\n return newError\n}\n\nfunction appendOwnerStack(error: Error) {\n if (!React.captureOwnerStack) {\n return\n }\n let stack = error.stack || ''\n // This module is only bundled in development mode so this is safe.\n const ownerStack = React.captureOwnerStack()\n // Avoid duplicate overriding stack frames\n if (ownerStack && stack.endsWith(ownerStack) === false) {\n stack += ownerStack\n // Override stack\n error.stack = stack\n }\n}\n"],"names":["getReactStitchedError","REACT_ERROR_STACK_BOTTOM_FRAME","REACT_ERROR_STACK_BOTTOM_FRAME_REGEX","RegExp","err","isErrorInstance","isError","originStack","stack","originMessage","message","stackLines","split","indexOfSplit","findIndex","line","test","isOriginalReactError","newStack","slice","join","newError","Error","Object","assign","copyNextErrorCode","appendOwnerStack","error","React","captureOwnerStack","ownerStack","endsWith"],"mappings":";;;;+BASgBA;;;eAAAA;;;;gEATE;kEACE;qCACc;AAElC,MAAMC,iCAAiC;AACvC,MAAMC,uCAAuC,IAAIC,OAC/C,AAAC,SAAMF,iCAA+B,SAAMA,iCAA+B;AAGtE,SAASD,sBAAmCI,GAAM;IACvD,MAAMC,kBAAkBC,IAAAA,gBAAO,EAACF;IAChC,MAAMG,cAAcF,kBAAkBD,IAAII,KAAK,IAAI,KAAK;IACxD,MAAMC,gBAAgBJ,kBAAkBD,IAAIM,OAAO,GAAG;IACtD,MAAMC,aAAaJ,YAAYK,KAAK,CAAC;IACrC,MAAMC,eAAeF,WAAWG,SAAS,CAAC,CAACC,OACzCb,qCAAqCc,IAAI,CAACD;IAE5C,MAAME,uBAAuBJ,gBAAgB,EAAE,mCAAmC;;IAClF,IAAIK,WAAWD,uBACXN,WAAWQ,KAAK,CAAC,GAAGN,cAAcO,IAAI,CAAC,QACvCb;IAEJ,MAAMc,WAAW,qBAAwB,CAAxB,IAAIC,MAAMb,gBAAV,qBAAA;eAAA;oBAAA;sBAAA;IAAuB;IACxC,8CAA8C;IAC9Cc,OAAOC,MAAM,CAACH,UAAUjB;IACxBqB,IAAAA,sCAAiB,EAACrB,KAAKiB;IACvBA,SAASb,KAAK,GAAGU;IAEjB,0CAA0C;IAC1CQ,iBAAiBL;IAEjB,OAAOA;AACT;AAEA,SAASK,iBAAiBC,KAAY;IACpC,IAAI,CAACC,cAAK,CAACC,iBAAiB,EAAE;QAC5B;IACF;IACA,IAAIrB,QAAQmB,MAAMnB,KAAK,IAAI;IAC3B,mEAAmE;IACnE,MAAMsB,aAAaF,cAAK,CAACC,iBAAiB;IAC1C,0CAA0C;IAC1C,IAAIC,cAActB,MAAMuB,QAAQ,CAACD,gBAAgB,OAAO;QACtDtB,SAASsB;QACT,iBAAiB;QACjBH,MAAMnB,KAAK,GAAGA;IAChB;AACF"}

View File

@@ -0,0 +1,4 @@
export type ErrorHandler = (error: Error) => void;
export declare function handleClientError(originError: unknown, consoleErrorArgs: any[], capturedFromConsole?: boolean): void;
export declare function useErrorHandler(handleOnUnhandledError: ErrorHandler, handleOnUnhandledRejection: ErrorHandler): void;
export declare function handleGlobalErrors(): void;

View File

@@ -0,0 +1,129 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
handleClientError: null,
handleGlobalErrors: null,
useErrorHandler: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
handleClientError: function() {
return handleClientError;
},
handleGlobalErrors: function() {
return handleGlobalErrors;
},
useErrorHandler: function() {
return useErrorHandler;
}
});
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
const _react = require("react");
const _attachhydrationerrorstate = require("./attach-hydration-error-state");
const _isnextroutererror = require("../is-next-router-error");
const _hydrationerrorinfo = require("./hydration-error-info");
const _console = require("../../lib/console");
const _iserror = /*#__PURE__*/ _interop_require_default._(require("../../../lib/is-error"));
const _consoleerror = require("./console-error");
const _enqueueclienterror = require("./enqueue-client-error");
const _stitchederror = require("../errors/stitched-error");
const queueMicroTask = globalThis.queueMicrotask || ((cb)=>Promise.resolve().then(cb));
const errorQueue = [];
const errorHandlers = [];
const rejectionQueue = [];
const rejectionHandlers = [];
function handleClientError(originError, consoleErrorArgs, capturedFromConsole) {
if (capturedFromConsole === void 0) capturedFromConsole = false;
let error;
if (!originError || !(0, _iserror.default)(originError)) {
// If it's not an error, format the args into an error
const formattedErrorMessage = (0, _console.formatConsoleArgs)(consoleErrorArgs);
const { environmentName } = (0, _console.parseConsoleArgs)(consoleErrorArgs);
error = (0, _consoleerror.createUnhandledError)(formattedErrorMessage, environmentName);
} else {
error = capturedFromConsole ? (0, _consoleerror.createUnhandledError)(originError) : originError;
}
error = (0, _stitchederror.getReactStitchedError)(error);
(0, _hydrationerrorinfo.storeHydrationErrorStateFromConsoleArgs)(...consoleErrorArgs);
(0, _attachhydrationerrorstate.attachHydrationErrorState)(error);
(0, _enqueueclienterror.enqueueConsecutiveDedupedError)(errorQueue, error);
for (const handler of errorHandlers){
// Delayed the error being passed to React Dev Overlay,
// avoid the state being synchronously updated in the component.
queueMicroTask(()=>{
handler(error);
});
}
}
function useErrorHandler(handleOnUnhandledError, handleOnUnhandledRejection) {
(0, _react.useEffect)(()=>{
// Handle queued errors.
errorQueue.forEach(handleOnUnhandledError);
rejectionQueue.forEach(handleOnUnhandledRejection);
// Listen to new errors.
errorHandlers.push(handleOnUnhandledError);
rejectionHandlers.push(handleOnUnhandledRejection);
return ()=>{
// Remove listeners.
errorHandlers.splice(errorHandlers.indexOf(handleOnUnhandledError), 1);
rejectionHandlers.splice(rejectionHandlers.indexOf(handleOnUnhandledRejection), 1);
// Reset error queues.
errorQueue.splice(0, errorQueue.length);
rejectionQueue.splice(0, rejectionQueue.length);
};
}, [
handleOnUnhandledError,
handleOnUnhandledRejection
]);
}
function onUnhandledError(event) {
if ((0, _isnextroutererror.isNextRouterError)(event.error)) {
event.preventDefault();
return false;
}
// When there's an error property present, we log the error to error overlay.
// Otherwise we don't do anything as it's not logging in the console either.
if (event.error) {
handleClientError(event.error, []);
}
}
function onUnhandledRejection(ev) {
const reason = ev == null ? void 0 : ev.reason;
if ((0, _isnextroutererror.isNextRouterError)(reason)) {
ev.preventDefault();
return;
}
let error = reason;
if (error && !(0, _iserror.default)(error)) {
error = (0, _consoleerror.createUnhandledError)(error + '');
}
rejectionQueue.push(error);
for (const handler of rejectionHandlers){
handler(error);
}
}
function handleGlobalErrors() {
if (typeof window !== 'undefined') {
try {
// Increase the number of stack frames on the client
Error.stackTraceLimit = 50;
} catch (e) {}
window.addEventListener('error', onUnhandledError);
window.addEventListener('unhandledrejection', onUnhandledRejection);
}
}
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
Object.defineProperty(exports.default, '__esModule', { value: true });
Object.assign(exports.default, exports);
module.exports = exports.default;
}
//# sourceMappingURL=use-error-handler.js.map

File diff suppressed because one or more lines are too long