A WebRTC error event is sent to an RTCDataChannel 对象的 onerror error handler when an error occurs on the data channel.

冒泡 Yes
可取消 No
接口 RTCErrorEvent
事件处理程序特性 onerror

RTCErrorEvent object provides details about the error that occurred; see that article for details.

范例

// Strings for each of the SCTP cause codes found in RFC
// 4960, section 3.3.10:
// https://tools.ietf.org/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 DLTS failure alert: ", err.receivedAlert);
      }
      if (err.sentAlert) {
        console.error("    Sent DLTS 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 an RTCError 对象称为 error ; RTCError is an extension of the DOMException interface. The error's 名称 is RTCError message is an error string specified by the WebRTC layer.

Error information is output to the console using console.error() 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 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 接口的 onerror 事件处理程序特性:

dc.onerror = ev => {
  const err = ev.error;
  /* ... */
}
					

注意: 由于 RTCError is not one of the legacy errors, the value of RTCError.code is always 0.

规范

规范 状态 注释
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'error event' in that specification.
候选推荐 最初的规范。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
error event Chrome 56 Edge ≤79 Firefox Yes IE No Opera 43 Safari ? WebView Android 56 Chrome Android 56 Firefox Android Yes Opera Android 43 Safari iOS No Samsung Internet Android 6.0

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: