script_bindings/
error.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use js::error::throw_type_error;
6use js::jsapi::JS_IsExceptionPending;
7
8use crate::codegen::PrototypeList::proto_id_to_name;
9use crate::num::Finite;
10use crate::script_runtime::JSContext as SafeJSContext;
11
12/// DOM exceptions that can be thrown by a native DOM method.
13/// <https://webidl.spec.whatwg.org/#dfn-error-names-table>
14#[derive(Clone, Debug, MallocSizeOf)]
15pub enum Error {
16    /// IndexSizeError DOMException
17    IndexSize(Option<String>),
18    /// NotFoundError DOMException
19    NotFound(Option<String>),
20    /// HierarchyRequestError DOMException
21    HierarchyRequest(Option<String>),
22    /// WrongDocumentError DOMException
23    WrongDocument(Option<String>),
24    /// InvalidCharacterError DOMException
25    InvalidCharacter(Option<String>),
26    /// NotSupportedError DOMException
27    NotSupported(Option<String>),
28    /// InUseAttributeError DOMException
29    InUseAttribute(Option<String>),
30    /// InvalidStateError DOMException
31    InvalidState(Option<String>),
32    /// SyntaxError DOMException
33    Syntax(Option<String>),
34    /// NamespaceError DOMException
35    Namespace(Option<String>),
36    /// InvalidAccessError DOMException
37    InvalidAccess(Option<String>),
38    /// SecurityError DOMException
39    Security(Option<String>),
40    /// NetworkError DOMException
41    Network(Option<String>),
42    /// AbortError DOMException
43    Abort(Option<String>),
44    /// TimeoutError DOMException
45    Timeout(Option<String>),
46    /// InvalidNodeTypeError DOMException
47    InvalidNodeType(Option<String>),
48    /// DataCloneError DOMException
49    DataClone(Option<String>),
50    /// TransactionInactiveError DOMException
51    TransactionInactive(Option<String>),
52    /// ReadOnlyError DOMException
53    ReadOnly(Option<String>),
54    /// VersionError DOMException
55    Version(Option<String>),
56    /// NoModificationAllowedError DOMException
57    NoModificationAllowed(Option<String>),
58    /// QuotaExceededError DOMException
59    QuotaExceeded {
60        quota: Option<Finite<f64>>,
61        requested: Option<Finite<f64>>,
62    },
63    /// TypeMismatchError DOMException
64    TypeMismatch(Option<String>),
65    /// InvalidModificationError DOMException
66    InvalidModification(Option<String>),
67    /// NotReadableError DOMException
68    NotReadable(Option<String>),
69    /// DataError DOMException
70    Data(Option<String>),
71    /// OperationError DOMException
72    Operation(Option<String>),
73    /// NotAllowedError DOMException
74    NotAllowed(Option<String>),
75    /// EncodingError DOMException
76    Encoding(Option<String>),
77    /// ConstraintError DOMException
78    Constraint(Option<String>),
79
80    /// TypeError JavaScript Error
81    Type(String),
82    /// RangeError JavaScript Error
83    Range(String),
84
85    /// A JavaScript exception is already pending.
86    JSFailed,
87}
88
89/// The return type for IDL operations that can throw DOM exceptions.
90pub type Fallible<T> = Result<T, Error>;
91
92/// The return type for IDL operations that can throw DOM exceptions and
93/// return `()`.
94pub type ErrorResult = Fallible<()>;
95
96/// Throw an exception to signal that a `JSObject` can not be converted to a
97/// given DOM type.
98pub fn throw_invalid_this(cx: SafeJSContext, proto_id: u16) {
99    debug_assert!(unsafe { !JS_IsExceptionPending(*cx) });
100    let error = format!(
101        "\"this\" object does not implement interface {}.",
102        proto_id_to_name(proto_id)
103    );
104    unsafe { throw_type_error(*cx, &error) };
105}
106
107pub fn throw_constructor_without_new(cx: SafeJSContext, name: &str) {
108    debug_assert!(unsafe { !JS_IsExceptionPending(*cx) });
109    let error = format!("{} constructor: 'new' is required", name);
110    unsafe { throw_type_error(*cx, &error) };
111}