diff --git a/files/en-us/_redirects.txt b/files/en-us/_redirects.txt index 8c93c9228fd5969..f9b98ea54652064 100644 --- a/files/en-us/_redirects.txt +++ b/files/en-us/_redirects.txt @@ -10741,6 +10741,7 @@ /en-US/docs/Web/API/range.startOffset /en-US/docs/Web/API/range/startOffset /en-US/docs/Web/API/range.surroundContents /en-US/docs/Web/API/range/surroundContents /en-US/docs/Web/API/range.toString /en-US/docs/Web/API/range/toString +/en-US/docs/Web/API/reportError /en-US/docs/Web/API/Window/reportError /en-US/docs/Web/API/scheduler_property /en-US/docs/Web/API/Window/scheduler /en-US/docs/Web/API/select.type /en-US/docs/Web/API/HTMLSelectElement/type /en-US/docs/Web/API/sessionStorage /en-US/docs/Web/API/Window/sessionStorage diff --git a/files/en-us/mozilla/firefox/releases/93/index.md b/files/en-us/mozilla/firefox/releases/93/index.md index f06225ecb7b7ce9..1627d890b8547ae 100644 --- a/files/en-us/mozilla/firefox/releases/93/index.md +++ b/files/en-us/mozilla/firefox/releases/93/index.md @@ -37,7 +37,7 @@ This article provides information about the changes in Firefox 93 that will affe - {{domxref("ElementInternals.shadowRoot")}} and {{domxref("HTMLElement.attachInternals")}} are now supported ([Firefox bug 1723521](https://bugzil.la/1723521)). - The value `device-pixel-content-box` is now supported for {{domxref("ResizeObserver.Observe()")}} ([Firefox bug 1587973](https://bugzil.la/1587973)). -- The {{domxref("reportError()")}} global function is now supported, allowing scripts to report errors to the console or global event handlers, emulating an uncaught JavaScript exception ([Firefox bug 1722448](https://bugzil.la/1722448)). +- The {{domxref("Window.reportError()")}} and {{domxref("WorkerGlobalScope.reportError()")}} is now supported, allowing scripts to report errors to the console or global event handlers, emulating an uncaught JavaScript exception ([Firefox bug 1722448](https://bugzil.la/1722448)). #### Events diff --git a/files/en-us/web/api/rtcicecandidate/usernamefragment/index.md b/files/en-us/web/api/rtcicecandidate/usernamefragment/index.md index 259c3056da3fd22..36cdd81e8ff53a1 100644 --- a/files/en-us/web/api/rtcicecandidate/usernamefragment/index.md +++ b/files/en-us/web/api/rtcicecandidate/usernamefragment/index.md @@ -82,7 +82,7 @@ const ssNewCandidate = (signalMsg) => { } } - pc.addIceCandidate(candidate).catch(reportError); + pc.addIceCandidate(candidate).catch(window.reportError); }; ``` diff --git a/files/en-us/web/api/rtcpeerconnection/setconfiguration/index.md b/files/en-us/web/api/rtcpeerconnection/setconfiguration/index.md index b2624fa5f7715ec..197da3c68ec435a 100644 --- a/files/en-us/web/api/rtcpeerconnection/setconfiguration/index.md +++ b/files/en-us/web/api/rtcpeerconnection/setconfiguration/index.md @@ -74,7 +74,7 @@ myPeerConnection .then(() => { // send the offer to the other peer using the signaling server }) - .catch(reportError); + .catch(window.reportError); ``` First, a new object is created, `restartConfig`, specifying the new ICE server and its credentials. diff --git a/files/en-us/web/api/rtcpeerconnection/setlocaldescription/index.md b/files/en-us/web/api/rtcpeerconnection/setlocaldescription/index.md index c32a24d50867aed..047387cc4051e1d 100644 --- a/files/en-us/web/api/rtcpeerconnection/setlocaldescription/index.md +++ b/files/en-us/web/api/rtcpeerconnection/setlocaldescription/index.md @@ -100,7 +100,7 @@ async function handleNegotiationNeededEvent() { pc.setLocalDescription(offer); signalRemotePeer({ description: pc.localDescription }); } catch (err) { - reportError(err); + window.reportError(err); } } ``` diff --git a/files/en-us/web/api/web_workers_api/functions_and_classes_available_to_workers/index.md b/files/en-us/web/api/web_workers_api/functions_and_classes_available_to_workers/index.md index 88adc1acba0ab3d..0af1bb1af500211 100644 --- a/files/en-us/web/api/web_workers_api/functions_and_classes_available_to_workers/index.md +++ b/files/en-us/web/api/web_workers_api/functions_and_classes_available_to_workers/index.md @@ -26,7 +26,7 @@ Some of the functions (a subset) that are common to all workers and to the main - {{domxref("WorkerGlobalScope.dump()", "dump()")}} {{non-standard_inline}} - {{domxref("WorkerGlobalScope.fetch()", "fetch()")}} - {{domxref("WorkerGlobalScope.queueMicrotask()", "queueMicrotask()")}} -- {{domxref("reportError()")}} +- {{domxref("WorkerGlobalScope.reportError()", "reportError()")}} - {{domxref("setInterval()")}} - {{domxref("setTimeout()")}} - {{domxref("structuredClone()")}} diff --git a/files/en-us/web/api/web_workers_api/index.md b/files/en-us/web/api/web_workers_api/index.md index 6b2efe9d43bfaf0..d2cace44509a26d 100644 --- a/files/en-us/web/api/web_workers_api/index.md +++ b/files/en-us/web/api/web_workers_api/index.md @@ -50,7 +50,7 @@ Some of the functions (a subset) that are common to all workers and to the main - {{domxref("WorkerGlobalScope.dump()", "dump()")}} {{non-standard_inline}} - {{domxref("WorkerGlobalScope.fetch()", "fetch()")}} - {{domxref("WorkerGlobalScope.queueMicrotask()", "queueMicrotask()")}} -- {{domxref("reportError()")}} +- {{domxref("WorkerGlobalScope.reportError()", "reportError()")}} - {{domxref("setInterval()")}} - {{domxref("setTimeout()")}} - {{domxref("structuredClone()")}} diff --git a/files/en-us/web/api/webrtc_api/signaling_and_video_calling/index.md b/files/en-us/web/api/webrtc_api/signaling_and_video_calling/index.md index a01eaa786107d89..19481c2352b0f3a 100644 --- a/files/en-us/web/api/webrtc_api/signaling_and_video_calling/index.md +++ b/files/en-us/web/api/webrtc_api/signaling_and_video_calling/index.md @@ -375,7 +375,7 @@ function handleNegotiationNeededEvent() { sdp: myPeerConnection.localDescription, }); }) - .catch(reportError); + .catch(window.reportError); } ``` @@ -397,7 +397,7 @@ We know the description is valid, and has been set, when the promise returned by - `sdp` - : The SDP string describing the offer. -If an error occurs, either in the initial `createOffer()` or in any of the fulfillment handlers that follow, an error is reported by invoking our `reportError()` function. +If an error occurs, either in the initial `createOffer()` or in any of the fulfillment handlers that follow, an error is reported by invoking our `window.reportError()` function. Once `setLocalDescription()`'s fulfillment handler has run, the ICE agent begins sending {{domxref("RTCPeerConnection.icecandidate_event", "icecandidate")}} events to the {{domxref("RTCPeerConnection")}}, one for each potential configuration it discovers. Our handler for the `icecandidate` event is responsible for transmitting the candidates to the other peer. @@ -462,7 +462,7 @@ Finally, the caller handles the answer message it received by creating a new {{d ```js function handleVideoAnswerMsg(msg) { const desc = new RTCSessionDescription(msg.sdp); - myPeerConnection.setRemoteDescription(desc).catch(reportError); + myPeerConnection.setRemoteDescription(desc).catch(window.reportError); } ``` @@ -506,7 +506,7 @@ The signaling server delivers each ICE candidate to the destination peer using w function handleNewICECandidateMsg(msg) { const candidate = new RTCIceCandidate(msg.candidate); - myPeerConnection.addIceCandidate(candidate).catch(reportError); + myPeerConnection.addIceCandidate(candidate).catch(window.reportError); } ``` diff --git a/files/en-us/web/api/window/index.md b/files/en-us/web/api/window/index.md index 3e7ed8352b72744..087758d01945b06 100644 --- a/files/en-us/web/api/window/index.md +++ b/files/en-us/web/api/window/index.md @@ -221,7 +221,7 @@ _This interface inherits methods from the {{domxref("EventTarget")}} interface._ - : Returns a {{jsxref("Promise")}} that fulfills with an array of {{domxref("FontData")}} objects representing the font faces available locally. - {{domxref("Window.queueMicrotask()")}} - : Queues a microtask to be executed at a safe time prior to control returning to the browser's event loop. -- {{domxref("reportError", "Window.reportError()")}} +- {{domxref("Window.reportError()")}} - : Reports an error in a script, emulating an unhandled exception. - {{domxref("Window.requestAnimationFrame()")}} - : Tells the browser that an animation is in progress, requesting that the browser schedule a repaint of the window for the next animation frame. diff --git a/files/en-us/web/api/window/reporterror/index.md b/files/en-us/web/api/window/reporterror/index.md new file mode 100644 index 000000000000000..b6e46763fc369eb --- /dev/null +++ b/files/en-us/web/api/window/reporterror/index.md @@ -0,0 +1,82 @@ +--- +title: "Window: reportError() method" +short-title: reportError() +slug: Web/API/Window/reportError +page-type: web-api-instance-method +browser-compat: api.reportError +--- + +{{APIRef("DOM")}} + +The **`reportError()`** method of the {{DOMxRef("Window")}} interface may be used to report errors to the console or event handlers of global scopes, 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 + +```js-nolint +reportError(throwable) +``` + +### Parameters + +- `throwable` + - : An error object such as a {{jsxref("TypeError")}}. + +### Return value + +None ({{jsxref("undefined")}}). + +### Exceptions + +- {{jsxref("TypeError")}} + - : The method is called without an error argument. + +## Examples + +Feature test for the method using: + +```js +if (typeof window.reportError === "function") { + // function is defined +} +``` + +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. + +```js +const newError = new Error("Some error message", "someFile.js", 11); +window.reportError(newError); + +window.onerror = (message, source, lineno, colno, error) => { + console.error(`message: ${error.message}, lineno: ${lineno}`); + return true; +}; + +window.addEventListener("error", (error) => { + console.error(error.filename); +}); + +// Output +// > "message:Some error message, lineno: 11" +// > "someFile.js" +``` + +## Specifications + +{{Specifications}} + +## Browser compatibility + +{{Compat}} + +## See also + +- {{DOMxRef("Window")}} +- {{DOMxRef("WorkerGlobalScope.reportError()")}} +- {{DOMxRef("Window/error_event", "error")}} event +- {{DOMxRef("WorkerGlobalScope/error_event", "error")}} event +- {{DOMxRef("HTMLElement/error_event", "error")}} event diff --git a/files/en-us/web/api/workerglobalscope/index.md b/files/en-us/web/api/workerglobalscope/index.md index b68b64f61a84f73..4a19698de06c2ae 100644 --- a/files/en-us/web/api/workerglobalscope/index.md +++ b/files/en-us/web/api/workerglobalscope/index.md @@ -74,7 +74,7 @@ _This interface inherits methods from the {{domxref("EventTarget")}} interface._ - : Schedules a function to execute in a given amount of time. - {{domxref("structuredClone()", "WorkerGlobalScope.structuredClone()")}} - : Creates a [deep clone](/en-US/docs/Glossary/Deep_copy) of a given value using the [structured clone algorithm](/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). -- {{domxref("reportError()", "WorkerGlobalScope.reportError()")}} +- {{domxref("WorkerGlobalScope.reportError()")}} - : Reports an error in a script, emulating an unhandled exception. ## Events diff --git a/files/en-us/web/api/reporterror/index.md b/files/en-us/web/api/workerglobalscope/reporterror/index.md similarity index 64% rename from files/en-us/web/api/reporterror/index.md rename to files/en-us/web/api/workerglobalscope/reporterror/index.md index 0f4c7da6bef1790..6859340f440ffdc 100644 --- a/files/en-us/web/api/reporterror/index.md +++ b/files/en-us/web/api/workerglobalscope/reporterror/index.md @@ -1,21 +1,19 @@ --- -title: reportError() global function +title: "WorkerGlobalScope: reportError() method" short-title: reportError() -slug: Web/API/reportError -page-type: web-api-global-function +slug: Web/API/WorkerGlobalScope/reportError +page-type: web-api-instance-method browser-compat: api.reportError --- -{{APIRef("DOM")}} {{AvailableInWorkers}} +{{APIRef("Web Workers API")}}{{AvailableInWorkers("worker")}} -The **`reportError()`** global method may be used to report errors to the console or global event handlers, emulating an uncaught JavaScript exception. +The **`reportError()`** method of the {{DOMxRef("WorkerGlobalScope")}} interface may be used to report errors to the console or event handlers of global scopes, 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 ```js-nolint @@ -46,14 +44,14 @@ 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. +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. ```js const newError = new Error("Some error message", "someFile.js", 11); self.reportError(newError); -window.onerror = (message, source, lineno, colno, error) => { +self.onerror = (message, source, lineno, colno, error) => { console.error(`message: ${error.message}, lineno: ${lineno}`); return true; }; @@ -77,6 +75,8 @@ self.addEventListener("error", (error) => { ## See also -- [`Window`](/en-US/docs/Web/API/Window) -- [`WorkerGlobalScope`](/en-US/docs/Web/API/WorkerGlobalScope) -- [error](/en-US/docs/Web/API/HTMLElement/error_event) event +- {{DOMxRef("WorkerGlobalScope")}} +- {{DOMxRef("Window.reportError()")}} +- {{DOMxRef("Window/error_event", "error")}} event +- {{DOMxRef("WorkerGlobalScope/error_event", "error")}} event +- {{DOMxRef("HTMLElement/error_event", "error")}} event diff --git a/files/jsondata/GroupData.json b/files/jsondata/GroupData.json index 3e77f944234732c..e7f60bce965fa74 100644 --- a/files/jsondata/GroupData.json +++ b/files/jsondata/GroupData.json @@ -792,7 +792,7 @@ "Window", "WorkletGlobalScope" ], - "methods": ["reportError"], + "methods": [], "properties": [], "events": [ "Document: DOMContentLoaded",