大网数据广告活动
Web 标准与前端开发

RTCDataChannel: error event

· 52CE 我爱测

A WebRTC `error` event is sent to an RTCDataChannel object's `onerror` event handler when an error occurs on the data channel.

WebRTC

A WebRTC error event is sent to an RTCDataChannel object's onerror event handler when an error occurs on the data channel.

The RTCErrorEvent 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 EventTarget/addEventListener:

addEventListener("error", (event) => { })

Alternatively, assign to the event handler property of the interface:

onerror = (event) => { }

Event type

An RTCErrorEvent. Inherits from Event.

RTCErrorEvent

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.10

const 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.sentAlert);
      }
      break;
  }

  // Add source file name and line information

  console.error(
    "    Error in file ",
    err.filename,
    " at line ",
    err.lineNumber,
    ", column ",
    err.columnNumber,
  );
});

The received event provides details in an RTCError object called RTCErrorEvent.error; RTCError is an extension of the DOMException interface. The error's DOMException.name is RTCError and the DOMException.message is an error string specified by the WebRTC layer.

Error information is output to the console using console/error_static. The message 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 of RTCError.errorDetail, 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 for error events using the RTCDataChannel interface's onerror event handler property (but note that this replaces other event listeners added using onerror, if any):

dc.onerror = (ev) => {
  const err = ev.error;

  // …
};

[!NOTE] Since RTCError is not one of the legacy errors, the value of DOMException.code is always 0.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。