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,
18 /// NotFoundError DOMException
19 NotFound,
20 /// HierarchyRequestError DOMException
21 HierarchyRequest,
22 /// WrongDocumentError DOMException
23 WrongDocument,
24 /// InvalidCharacterError DOMException
25 InvalidCharacter,
26 /// NotSupportedError DOMException
27 NotSupported,
28 /// InUseAttributeError DOMException
29 InUseAttribute,
30 /// InvalidStateError DOMException
31 InvalidState,
32 /// SyntaxError DOMException
33 Syntax,
34 /// NamespaceError DOMException
35 Namespace,
36 /// InvalidAccessError DOMException
37 InvalidAccess,
38 /// SecurityError DOMException
39 Security,
40 /// NetworkError DOMException
41 Network,
42 /// AbortError DOMException
43 Abort,
44 /// TimeoutError DOMException
45 Timeout,
46 /// InvalidNodeTypeError DOMException
47 InvalidNodeType,
48 /// DataCloneError DOMException
49 DataClone(Option<String>),
50 /// TransactionInactiveError DOMException
51 TransactionInactive,
52 /// ReadOnlyError DOMException
53 ReadOnly,
54 /// VersionError DOMException
55 Version,
56 /// NoModificationAllowedError DOMException
57 NoModificationAllowed,
58 /// QuotaExceededError DOMException
59 QuotaExceeded {
60 quota: Option<Finite<f64>>,
61 requested: Option<Finite<f64>>,
62 },
63 /// TypeMismatchError DOMException
64 TypeMismatch,
65 /// InvalidModificationError DOMException
66 InvalidModification,
67 /// NotReadableError DOMException
68 NotReadable,
69 /// DataError DOMException
70 Data,
71 /// OperationError DOMException
72 Operation,
73 /// NotAllowedError DOMException
74 NotAllowed,
75 /// EncodingError DOMException
76 Encoding,
77 /// ConstraintError DOMException
78 Constraint,
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}