Skip to content

Commit d1b3f12

Browse files
committed
Handle errors in index.html
1 parent a9d0852 commit d1b3f12

File tree

4 files changed

+453
-291
lines changed

4 files changed

+453
-291
lines changed

client/modules/IDE/components/Editor.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,12 @@ class Editor extends React.Component {
262262
.split('\n')[1]
263263
.split('(')[1]
264264
.split(')')[0];
265-
} else {
265+
} else if (consoleEvent.data[0].indexOf('at ') > -1) {
266266
sourceAndLoc = consoleEvent.data[0] // eslint-disable-line
267267
.split('\n')[1]
268268
.split('at ')[1];
269+
} else {
270+
return;
269271
}
270272
const [source, line] = sourceAndLoc.split(':');
271273

client/utils/previewEntry.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import loopProtect from 'loop-protect';
22
import { Hook, Decode, Encode } from 'console-feed';
33
import evaluateExpression from './evaluateExpression';
4-
import { EXTERNAL_LINK_REGEX } from '../../server/utils/fileUtils';
54

65
// should postMessage user the dispatcher? does the parent window need to
76
// be registered as a frame? or a just a listener?
@@ -10,6 +9,7 @@ import { EXTERNAL_LINK_REGEX } from '../../server/utils/fileUtils';
109
// const { editor } = window;
1110
const editor = window.parent.parent;
1211
const { editorOrigin } = window;
12+
const htmlOffset = 12;
1313
// const editorOrigin = '*';
1414
// const editorOrigin = 'http://localhost:8000';
1515
// so this works??
@@ -77,12 +77,16 @@ function getScriptOff(line) {
7777
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108
7878
window.onerror = function onError(msg, source, lineNumber, columnNo, error) {
7979
const urls = Object.keys(window.objectUrls);
80-
let data = '';
80+
let data = error.stack;
8181
urls.forEach((url) => {
8282
if (error.stack.match(url)) {
8383
data = error.stack.replaceAll(url, window.objectUrls[url]);
8484
}
8585
});
86+
if (data.match('about:srcdoc')) {
87+
data = data.replaceAll('about:srcdoc', 'index.html');
88+
data = data.replace(`:${lineNumber}:`, `:${lineNumber - htmlOffset}:`);
89+
}
8690
editor.postMessage(
8791
{
8892
source: 'sketch',
@@ -106,12 +110,13 @@ window.onerror = function onError(msg, source, lineNumber, columnNo, error) {
106110
window.onunhandledrejection = function onUnhandledRejection(event) {
107111
if (event.reason && event.reason.message && event.reason.stack) {
108112
const urls = Object.keys(window.objectUrls);
109-
let data = '';
113+
let data = event.reason.stack;
110114
urls.forEach((url) => {
111115
if (event.reason.stack.match(url)) {
112116
data = event.reason.stack.replaceAll(url, window.objectUrls[url]);
113117
}
114118
});
119+
data = data.replaceAll('about:srcdoc', 'index.html');
115120
editor.postMessage(
116121
{
117122
source: 'sketch',

0 commit comments

Comments
 (0)