diff --git a/src/core/mount.js b/src/core/mount.js index 8f3a37ef77..c4d0ba9d52 100644 --- a/src/core/mount.js +++ b/src/core/mount.js @@ -6,7 +6,7 @@ import Store from "../components/store"; import { persistor, store } from "./store"; import ErrorBoundaryAlert from "../components/error-boundary-alert/ErrorBoundaryAlert"; import { closeLastModal } from "./modal.slice"; -import extractErrorDetails from "./utils/extractErrorDetails"; +import forwardError from "./utils/forwardError"; /** * We look for containers and corresponding applications. @@ -39,23 +39,7 @@ function mount(context) { // eslint-disable-next-line no-console console.error(error, info); - const { filename, lineNumber, column } = extractErrorDetails( - info.componentStack - ); - - // Call window.onerror to send the error to the error logging system. - if (window.onerror) { - window.onerror( - `${error.name}: ${error.message}`, - filename, - lineNumber, - column, - { - ...info, - stack: info.componentStack - } - ); - } + forwardError(error, info); } }), { diff --git a/src/core/utils/extractErrorDetails.js b/src/core/utils/extractErrorDetails.js index bec4f8ad9b..f9796584b9 100644 --- a/src/core/utils/extractErrorDetails.js +++ b/src/core/utils/extractErrorDetails.js @@ -8,12 +8,14 @@ const extractErrorDetails = (stack) => { const stackLines = (stack || "").split("\n"); const match = stackLines .map((line) => { + // Extract filename, lineNumber, and column from a string formatted like "filename:line:column" const parts = line.match(/(?:\()?(.*?):(\d+):(\d+)(?:\))?/); if (parts) { + const [, filename, lineNumber, column] = parts; return { - filename: parts[1], - lineNumber: parts[2], - column: parts[3] + filename, + lineNumber, + column }; } return null; diff --git a/src/core/utils/forwardError.js b/src/core/utils/forwardError.js new file mode 100644 index 0000000000..26db9968cd --- /dev/null +++ b/src/core/utils/forwardError.js @@ -0,0 +1,29 @@ +import extractErrorDetails from "./extractErrorDetails"; + +/** + * Forwards an error to the window.onerror handler. + * + * @param {Error} error - The error object. + * @param {Object} info - Additional info, including component stack trace. + */ +const forwardError = (error, info) => { + const { filename, lineNumber, column } = extractErrorDetails( + info.componentStack + ); + + // Call window.onerror to send the error to the error logging system. + if (window.onerror) { + window.onerror( + `${error.name}: ${error.message}`, + filename, + lineNumber, + column, + { + ...info, + stack: info.componentStack + } + ); + } +}; + +export default forwardError;