reportError() global function
The reportError()
global method may be used to report errors to the console or global event handlers, emulating an uncaught JavaScript exception.
This feature is primarily intended for custom event-dispatching or callback-manipulating libraries. Libraries can use this feature to catch errors in callback code and re-throw them to the top level handler. This ensures that an exception in one callback will not prevent others from being handled, while at the same time ensuring that stack trace information is still readily available for debugging at the top level.
Syntax
Parameters
-
throwable
-
An error object such as a TypeError
.
Return value
Exceptions
-
TypeError
-
The method is called without an error argument.
Examples
Feature test for the method using:
if (typeof self.reportError === "function") {
}
The following code shows how you might create and report an error, and how it may be caught using either the global onerror
handler or by adding a listener for the error
event. Note that the handler assigned to onerror
must return true
to stop the event propagating further.
const newError = new Error("Some error message", "someFile.js", 11);
self.reportError(newError);
window.onerror = (message, source, lineno, colno, error) => {
console.error(`message: ${error.message}, lineno: ${lineno}`);
return true;
};
self.addEventListener("error", (error) => {
console.error(error.filename);
});
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
reportError |
95 |
95 |
93 |
No |
81 |
15.4 |
95 |
95 |
93 |
67 |
15.4 |
17.0 |
worker_support |
95 |
95 |
93 |
No |
81 |
No |
95 |
95 |
93 |
67 |
No |
17.0 |
See also