Skip to main content

rtci_host/
error.rs

1// Copyright 2026 Brainwires. MIT OR Apache-2.0.
2
3//! The crate's error: a W3C / Web IDL exception kind and its message. Each
4//! front end maps [`ErrorKind`] to its own error classes (a `JsError` in the
5//! built-in extension, integer codes over FFI).
6
7use std::fmt;
8
9/// Which exception a failure is, as the W3C WebRTC specs name them.
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
11#[non_exhaustive]
12pub enum ErrorKind {
13  /// A `TypeError`.
14  Type,
15  /// A `DOMException` named `SyntaxError`.
16  Syntax,
17  /// A `DOMException` named `InvalidAccessError`.
18  InvalidAccess,
19  /// A `DOMException` named `InvalidStateError`.
20  InvalidState,
21  /// A `DOMException` named `OperationError`.
22  Operation,
23  /// A `DOMException` named `InvalidModificationError`.
24  InvalidModification,
25  /// A `DOMException` named `NotSupportedError`.
26  NotSupported,
27  /// The [`NetGate`](crate::NetGate) refused an address (a permission
28  /// denial: Deno's `NotCapable`).
29  NotCapable,
30  /// A failure the front end should report as an internal error (no W3C
31  /// class fits).
32  Internal,
33}
34
35impl ErrorKind {
36  /// A stable name for the kind (`"TypeError"`, `"InvalidStateError"`, ...),
37  /// the `DOMException` name where there is one.
38  pub fn name(self) -> &'static str {
39    match self {
40      ErrorKind::Type => "TypeError",
41      ErrorKind::Syntax => "SyntaxError",
42      ErrorKind::InvalidAccess => "InvalidAccessError",
43      ErrorKind::InvalidState => "InvalidStateError",
44      ErrorKind::Operation => "OperationError",
45      ErrorKind::InvalidModification => "InvalidModificationError",
46      ErrorKind::NotSupported => "NotSupportedError",
47      ErrorKind::NotCapable => "NotCapable",
48      ErrorKind::Internal => "Error",
49    }
50  }
51}
52
53/// A failure of an `rtci-host` call: its kind and message.
54#[derive(Clone, Debug, PartialEq, Eq)]
55pub struct Error {
56  pub kind: ErrorKind,
57  pub message: String,
58}
59
60impl Error {
61  pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
62    Self {
63      kind,
64      message: message.into(),
65    }
66  }
67
68  /// A [`ErrorKind::NotCapable`] error: the gate refused (the message is the
69  /// gate's).
70  pub fn not_capable(message: impl Into<String>) -> Self {
71    Self::new(ErrorKind::NotCapable, message)
72  }
73
74  pub fn kind(&self) -> ErrorKind {
75    self.kind
76  }
77
78  pub fn message(&self) -> &str {
79    &self.message
80  }
81}
82
83impl fmt::Display for Error {
84  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85    f.write_str(&self.message)
86  }
87}
88
89impl std::error::Error for Error {}
90
91/// `{"kind": <ErrorKind::name>, "message": ...}` (the `error` of an
92/// `opdone` event).
93impl serde::Serialize for Error {
94  fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
95    use serde::ser::SerializeStruct;
96    let mut st = s.serialize_struct("Error", 2)?;
97    st.serialize_field("kind", self.kind.name())?;
98    st.serialize_field("message", &self.message)?;
99    st.end()
100  }
101}
102
103/// `Result` with this crate's [`Error`].
104pub type Result<T, E = Error> = std::result::Result<T, E>;