From 111abb2b9308f1b6ccbcbc15abab484e5baa174b Mon Sep 17 00:00:00 2001 From: ridait Date: Wed, 26 Aug 2020 19:15:21 +0200 Subject: [PATCH] Fix capture and display rejected promises in web editor console --- client/utils/consoleUtils.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/client/utils/consoleUtils.js b/client/utils/consoleUtils.js index a6013924..19e942e8 100644 --- a/client/utils/consoleUtils.js +++ b/client/utils/consoleUtils.js @@ -38,6 +38,22 @@ export const hijackConsoleErrorsScript = (offs) => { }], '*'); return false; }; + // catch rejected promises + window.onunhandledrejection = function (event) { + if (event.reason && event.reason.message && event.reason.stack){ + var errorNum = event.reason.stack.split('about:srcdoc:')[1].split(':')[0]; + var fileInfo = getScriptOff(errorNum); + var data = event.reason.message + ' (' + fileInfo[1] + ': line ' + fileInfo[0] + ')'; + window.parent.postMessage([{ + log: [{ + method: 'error', + data: [data], + id: Date.now().toString() + }], + source: fileInfo[1] + }], '*'); + } + }; `; return s; }; @@ -46,7 +62,7 @@ export const startTag = '@fs-'; export const getAllScriptOffsets = (htmlFile) => { const offs = []; - const hijackConsoleErrorsScriptLength = 36; + const hijackConsoleErrorsScriptLength = 52; const embeddedJSStart = 'script crossorigin=""'; let foundJSScript = true; let foundEmbeddedJS = true;