Skip to content

Commit dafef7b

Browse files
committed
Handle errors with no stack trace, like SyntaxError
Show URL and line/column numbers for errors with no call stack.
1 parent a9fe9d3 commit dafef7b

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/error-handling-enhanced.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,26 @@ import { show_error_message } from "./functions.js";
1616
var old_onerror = window.onerror;
1717
window.onerror = function (message, source, lineno, colno, error) {
1818
try {
19-
// Some errors don't give an error object, like "ResizeObserver loop limit exceeded"
20-
// @ts-ignore
21-
show_error_message(localize("Internal application error."), error || message);
19+
/** @type {Error | string} */
20+
var error_details = "";
21+
if (!error) {
22+
// Some errors don't give an error object, like "ResizeObserver loop limit exceeded"
23+
// in which case the message should be used.
24+
// The source/lineno/colno arguments are not helpful in this case.
25+
// To test this: copy code (excluding onerror handler) from this nice repo: https://github.com/OliverJAsh/resize-observer-loop-tests/blob/6a9a5bd8e443c38d3df63833fc7e04abfc6fec32/simple.html#L8-L25
26+
// (BTW: I could specifically check that `source === location.href` to ignore the source/lineno/colno arguments, and otherwise show them...)
27+
error_details = /** @type {string} */(message); // This is a string, never an Event. Event is for onerror of images etc.
28+
} else if ("stack" in error && error.stack.indexOf(source) === -1) {
29+
// Some errors give an error object without a stack trace, like "SyntaxError: Unexpected reserved word",
30+
// but the source/lineno/colno are actually available.
31+
// There's no stack trace because it hasn't started executing yet.
32+
// Still it would be nice if they gave a "fake" stack trace with the source/lineno/colno,
33+
// so we don't have to handle this special case.
34+
error_details = `${message}\n\nURL: ${source}\nLine: ${lineno}\nColumn: ${colno}`;
35+
} else {
36+
error_details = error;
37+
}
38+
show_error_message(localize("Internal application error."), error_details);
2239
} catch (e) {
2340
old_onerror(message, source, lineno, colno, error);
2441
console.warn("Error in error handler:", e);

0 commit comments

Comments
 (0)