diff --git a/files/en-us/web/api/window/reporterror/index.md b/files/en-us/web/api/window/reporterror/index.md index ebf2e6e5370ea58..326b5af445db1f9 100644 --- a/files/en-us/web/api/window/reporterror/index.md +++ b/files/en-us/web/api/window/reporterror/index.md @@ -23,7 +23,7 @@ reportError(throwable) ### Parameters - `throwable` - - : An error object such as a {{jsxref("TypeError")}}. + - : Any JavaScript value, but preferably an error object such as a {{jsxref("TypeError")}}. ### Return value @@ -36,6 +36,8 @@ None ({{jsxref("undefined")}}). ## Examples +### Feature detection + Feature test for the method using: ```js @@ -44,27 +46,41 @@ if (typeof window.reportError === "function") { } ``` -The following code shows how you might create and report an error, and how it may be caught using either the `onerror` event handler property 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. +### Telemetry -```js -const newError = new Error("Some error message", "someFile.js", 11); -window.reportError(newError); +`reportError()` allows asynchronous errors to be reported just like built-in errors. Aggregating all errors at a single point makes it easier to collect telemetry about errors occurring in an application. -window.onerror = (message, source, lineno, colno, error) => { - console.error(`message: ${error.message}, lineno: ${lineno}`); - return true; -}; +For example, a web application might set up a global [`error`](/en-US/docs/Web/API/Window/error_event) event listener to collect all uncaught errors, and send them to a server for analysis, such as by using [Sentry](https://sentry.io/): -window.addEventListener("error", (error) => { - console.error(error.filename); +```js +window.addEventListener("error", (event) => { + event.preventDefault(); // Prevent the default logging to console + Sentry.captureException(event.error); + console.error("Error encountered:", event.error); + showToastNotification("An error occurred. Our team has been notified."); }); +``` -// Output -// > "message:Some error message, lineno: 11" -// > "someFile.js" +By default, this listener can listen for uncaught exceptions thrown in synchronous `