diff --git a/scripts/aem.js b/scripts/aem.js index de8a279dc7..669a1a7734 100644 --- a/scripts/aem.js +++ b/scripts/aem.js @@ -32,23 +32,39 @@ function sampleRUM(checkpoint, data) { collector: (...args) => window.hlx.rum.queue.push(args), }; if (isSelected) { - ['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); - }); + const dataFromErrorObj = (error) => { + const errData = { source: 'undefined error' }; + try { + errData.target = error.toString(); + errData.source = error.stack + .split('\n') + .filter((line) => line.match(/https?:\/\//)) + .shift() + .replace(/at ([^ ]+) \((.+)\)/, '$1@$2') + .replace(/ at /, '@') + .trim(); + } catch (err) { + /* error structure was not as expected */ + } + return errData; + }; + + window.addEventListener('error', ({ error }) => { + const errData = dataFromErrorObj(error); + sampleRUM('error', errData); }); + + window.addEventListener('unhandledrejection', ({ reason }) => { + let errData = { + source: 'Unhandled Rejection', + target: reason || 'Unknown', + }; + if (reason instanceof Error) { + errData = dataFromErrorObj(reason); + } + sampleRUM('error', errData); + }); + sampleRUM.baseURL = sampleRUM.baseURL || new URL(window.RUM_BASE || '/', new URL('https://rum.hlx.page')); sampleRUM.collectBaseURL = sampleRUM.collectBaseURL || sampleRUM.baseURL; sampleRUM.sendPing = (ck, time, pingData = {}) => {