RTCDataChannel: error event
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
A WebRTCerror
event is sent to anRTCDataChannel
object'sonerror
event handler when an error occurs on the data channel.
TheRTCErrorEvent
object provides details about the error that occurred; see that article for details.
This event is not cancelable and does not bubble.
Syntax
Use the event name in methods likeaddEventListener()
, or set an event handler property.
addEventListener("error", (event) => { })onerror = (event) => { }
Event type
AnRTCErrorEvent
. Inherits fromEvent
.
Event properties
In addition to the properties listed below, properties from the parent interface,Event
, are available.
Examples
// Strings for each of the SCTP cause codes found in RFC// 4960, section 3.3.10:// https://datatracker.ietf.org/doc/html/rfc4960#section-3.3.10const sctpCauseCodes = [ "No SCTP error", "Invalid stream identifier", "Missing mandatory parameter", "Stale cookie error", "Sender is out of resource (i.e., memory)", "Unable to resolve address", "Unrecognized SCTP chunk type received", "Invalid mandatory parameter", "Unrecognized parameters", "No user data (SCTP DATA chunk has no data)", "Cookie received while shutting down", "Restart of an association with new addresses", "User-initiated abort", "Protocol violation",];dc.addEventListener( "error", (ev) => { const err = ev.error; console.error("WebRTC error: ", err.message); // Handle specific error detail types switch (err.errorDetail) { case "sdp-syntax-error": console.error(" SDP syntax error in line ", err.sdpLineNumber); break; case "idp-load-failure": console.error( " Identity provider load failure: HTTP error ", err.httpRequestStatusCode, ); break; case "sctp-failure": if (err.sctpCauseCode < sctpCauseCodes.length) { console.error(" SCTP failure: ", err.sctpCauseCode); } else { console.error(" Unknown SCTP error"); } break; case "dtls-failure": if (err.receivedAlert) { console.error(" Received DTLS failure alert: ", err.receivedAlert); } if (err.sentAlert) { console.error(" Sent DTLS failure alert: ", err.receivedAlert); } break; } // Add source file name and line information console.error( " Error in file ", err.filename, " at line ", err.lineNumber, ", column ", err.columnNumber, ); }, false,);
The received event provides details in anRTCError
object callederror
;RTCError
is an extension of theDOMException
interface. The error'sname
isRTCError
and themessage
is an error string specified by the WebRTC layer.
Error information is output to the console usingconsole.error()
. Themessage
string is always output, as is information about the source file's name, line number, and column number at which the error occurred.
In addition, however, depending on the value oferrorDetail
, additional information may be output. Each error type has a different set of information output. For example, an SDP syntax error displays the line number of the error within the SDP, and an SCTP error displays a message corresponding to the SCTP cause code. Other error types similarly output appropriate information.
You can also set up an event handler forerror
events using theRTCDataChannel
interface'sonerror
event handler property:
dc.onerror = (ev) => { const err = ev.error; // …};
Note:SinceRTCError
is not one of the legacy errors, the value ofRTCError.code
is always 0.
Specifications
Specification |
---|
WebRTC: Real-Time Communication in Browsers # event-datachannel-error |
WebRTC: Real-Time Communication in Browsers # dom-rtcdatachannel-onerror |
Browser compatibility
See also
- WebRTC API
- A simple RTCDataChannel example
- Related events:
open
,message
, andclose