/
runtime-script-errors.js
68 lines (55 loc) · 2.19 KB
/
runtime-script-errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"use strict";
const util = require("util");
const ErrorEvent = require("../generated/ErrorEvent");
const errorReportingMode = Symbol("error reporting mode");
// https://html.spec.whatwg.org/multipage/webappapis.html#report-the-error
// Omits script parameter and any check for muted errors; takes error object, message, and location as params, unlike
// the spec. Returns whether the event was handled or not.
function reportAnError(line, col, target, errorObject, message, location) {
if (target[errorReportingMode]) {
return false;
}
target[errorReportingMode] = true;
// TODO Events: use constructor directly, once they are no longer tied to a window.
const event = ErrorEvent.createImpl(["error", {
bubbles: false,
cancelable: true,
message,
filename: location,
lineno: line,
colno: col,
error: errorObject
}]);
try {
target.dispatchEvent(event);
} finally {
target[errorReportingMode] = false;
return event.defaultPrevented;
}
}
module.exports = function reportException(window, error, filenameHint) {
// This function will give good results on real Error objects with stacks; poor ones otherwise
const stack = error && error.stack;
const lines = stack && stack.split("\n");
// Find the first line that matches; important for multi-line messages
let pieces;
if (lines) {
for (let i = 1; i < lines.length && !pieces; ++i) {
pieces = lines[i].match(/at (?:(.+)\s+)?\(?(?:(.+?):(\d+):(\d+)|([^)]+))\)?/);
}
}
const fileName = pieces && pieces[2] || filenameHint || window._document.URL;
const lineNumber = pieces && parseInt(pieces[3]) || 0;
const columnNumber = pieces && parseInt(pieces[4]) || 0;
const handled = reportAnError(lineNumber, columnNumber, window, error, error.message, fileName);
if (!handled) {
const errorString = shouldBeDisplayedAsError(error) ? `[${error.name}: ${error.message}]` : util.inspect(error);
const jsdomError = new Error(`Uncaught ${errorString}`);
jsdomError.detail = error;
jsdomError.type = "unhandled exception";
window._virtualConsole.emit("jsdomError", jsdomError);
}
};
function shouldBeDisplayedAsError(x) {
return x.name && x.message !== undefined && x.stack;
}