-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1257 from danskernesdigitalebibliotek/feature/DDF…
…LSBP-569-logging-i-frontend Forward exceptions to window.onerror [DDFLSBP-569]
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Extracts error details from a stack trace. | ||
* | ||
* @param {string} stack - The stack trace. | ||
* @returns {Object} An object containing filename, lineNumber, and column. | ||
*/ | ||
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, | ||
lineNumber, | ||
column | ||
}; | ||
} | ||
return null; | ||
}) | ||
.find((m) => m); | ||
return ( | ||
match || { | ||
filename: "unknown", | ||
lineNumber: "0", | ||
column: "0" | ||
} | ||
); | ||
}; | ||
|
||
export default extractErrorDetails; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |