Skip to content

Commit

Permalink
feat: better error tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Jun 27, 2024
1 parent 232d7d7 commit 608c86e
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,18 @@ function init() {

window.addEventListener('load', () => sampleRUM('load'));

window.addEventListener('unhandledrejection', (event) => {
sampleRUM('error', { source: event.reason.sourceURL, target: event.reason.line });
});

window.addEventListener('error', (event) => {
sampleRUM('error', { source: event.filename, target: event.lineno });
['error', 'unhandledrejection'].forEach((event) => {
window.addEventListener(event, ({ reason, error }) => {
const errData = { source: 'undefined error' };
try {
errData.target = (reason || error).toString();
errData.source = (reason || error).stack.split('\n')
.filter((line) => line.match(/https?:\/\//)).shift()
.replace(/at ([^ ]+) \((.+)\)/, '$1@$2')
.trim();
} catch (err) { /* error structure was not as expected */ }
sampleRUM('error', errData);
});
});
}

Expand Down

0 comments on commit 608c86e

Please sign in to comment.