Skip to content

Commit

Permalink
refactor: moved forwardError to its own file, updated extractErrorDet…
Browse files Browse the repository at this point in the history
…ails to use array destructuring, added comments
  • Loading branch information
clausbruun committed Jun 7, 2024
1 parent 36d5aae commit 7e70f4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
20 changes: 2 additions & 18 deletions src/core/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}),
{
Expand Down
8 changes: 5 additions & 3 deletions src/core/utils/extractErrorDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions src/core/utils/forwardError.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 7e70f4b

Please sign in to comment.