Skip to content

Commit

Permalink
Fix globals, part 12: reportError() (mdn#35927)
Browse files Browse the repository at this point in the history
* move

* update

* Update index.md

* Update index.md

---------

Co-authored-by: wbamberg <will@bootbonnet.ca>
  • Loading branch information
2 people authored and OnkarRuikar committed Sep 28, 2024
1 parent d601cde commit b9bf9d4
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 25 deletions.
1 change: 1 addition & 0 deletions files/en-us/_redirects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/mozilla/firefox/releases/93/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const ssNewCandidate = (signalMsg) => {
}
}

pc.addIceCandidate(candidate).catch(reportError);
pc.addIceCandidate(candidate).catch(window.reportError);
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async function handleNegotiationNeededEvent() {
pc.setLocalDescription(offer);
signalRemotePeer({ description: pc.localDescription });
} catch (err) {
reportError(err);
window.reportError(err);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()")}}
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/api/web_workers_api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()")}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ function handleNegotiationNeededEvent() {
sdp: myPeerConnection.localDescription,
});
})
.catch(reportError);
.catch(window.reportError);
}
```

Expand All @@ -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.

Expand Down Expand Up @@ -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);
}
```

Expand Down Expand Up @@ -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);
}
```

Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/api/window/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
82 changes: 82 additions & 0 deletions files/en-us/web/api/window/reporterror/index.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion files/en-us/web/api/workerglobalscope/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

<!-- {{EmbedInteractiveExample("pages/js/self-reporterror.html")}} -->

## Syntax

```js-nolint
Expand Down Expand Up @@ -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;
};
Expand All @@ -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
2 changes: 1 addition & 1 deletion files/jsondata/GroupData.json
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@
"Window",
"WorkletGlobalScope"
],
"methods": ["reportError"],
"methods": [],
"properties": [],
"events": [
"Document: DOMContentLoaded",
Expand Down

0 comments on commit b9bf9d4

Please sign in to comment.