stdweb/webapi/
dom_exception.rs

1use webcore::value::Reference;
2use webapi::error::{IError, Error};
3
4/// The `IDomException` interface represents an abnormal event which occurs as the result of
5/// calling a web API.
6///
7/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/DOMException)
8// https://heycam.github.io/webidl/#idl-DOMException
9pub trait IDomException: IError {}
10
11/// A reference to a JavaScript `DOMException` object.
12///
13/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/DOMException)
14// https://heycam.github.io/webidl/#idl-DOMException
15#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
16#[reference(instance_of = "DOMException")]
17#[reference(subclass_of(Error))]
18pub struct DomException( Reference );
19
20impl IError for DomException {}
21impl IDomException for DomException {}
22
23error_boilerplate! { DomException }
24
25/// Occurs when an operation would result in an incorrect node tree.
26// https://heycam.github.io/webidl/#hierarchyrequesterror
27#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
28#[reference(subclass_of(Error, DomException))]
29pub struct HierarchyRequestError( Reference );
30
31impl IError for HierarchyRequestError {}
32impl IDomException for HierarchyRequestError {}
33
34error_boilerplate! { HierarchyRequestError, dom_exception = "HierarchyRequestError" }
35
36/// Occurs when an object does not support an operation or argument.
37// https://heycam.github.io/webidl/#invalidaccesserror
38#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
39#[reference(subclass_of(Error, DomException))]
40pub struct InvalidAccessError( Reference );
41
42impl IError for InvalidAccessError {}
43impl IDomException for InvalidAccessError {}
44
45error_boilerplate! { InvalidAccessError, dom_exception = "InvalidAccessError" }
46
47/// Occurs when the object can not be modified.
48// https://heycam.github.io/webidl/#nomodificationallowederror
49#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
50#[reference(subclass_of(Error, DomException))]
51pub struct NoModificationAllowedError( Reference );
52
53impl IError for NoModificationAllowedError {}
54impl IDomException for NoModificationAllowedError {}
55
56error_boilerplate! { NoModificationAllowedError, dom_exception = "NoModificationAllowedError" }
57
58/// Occurs when the specified object cannot be found.
59// https://heycam.github.io/webidl/#notfounderror
60#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
61#[reference(subclass_of(Error, DomException))]
62pub struct NotFoundError( Reference );
63
64impl IError for NotFoundError {}
65impl IDomException for NotFoundError {}
66
67error_boilerplate! { NotFoundError, dom_exception = "NotFoundError" }
68
69/// Occurs when the requested operation is insecure.
70// https://heycam.github.io/webidl/#securityerror
71#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
72#[reference(subclass_of(Error, DomException))]
73pub struct SecurityError( Reference );
74
75impl IError for SecurityError {}
76impl IDomException for SecurityError {}
77
78error_boilerplate! { SecurityError, dom_exception = "SecurityError" }
79
80/// Occurs when an argument does not match the expected pattern.
81// https://heycam.github.io/webidl/#syntaxerror
82#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
83#[reference(subclass_of(Error, DomException))]
84pub struct SyntaxError( Reference );
85
86impl IError for SyntaxError {}
87impl IDomException for SyntaxError {}
88
89error_boilerplate! { SyntaxError, dom_exception = "SyntaxError" }
90
91/// Occurs when an argument is out of range.
92// https://heycam.github.io/webidl/#indexsizeerror
93#[derive(Clone, Debug, ReferenceType)]
94#[reference(subclass_of(Error, DomException))]
95pub struct IndexSizeError( Reference );
96
97impl IError for IndexSizeError {}
98impl IDomException for IndexSizeError {}
99
100error_boilerplate! { IndexSizeError, dom_exception = "IndexSizeError" }
101
102/// Occurs when an object is in an invalid state.
103// https://heycam.github.io/webidl/#invalidstateerror
104#[derive(Clone, Debug, ReferenceType)]
105#[reference(subclass_of(Error, DomException))]
106pub struct InvalidStateError( Reference );
107
108impl IError for InvalidStateError {}
109impl IDomException for InvalidStateError {}
110
111error_boilerplate! { InvalidStateError, dom_exception = "InvalidStateError" }
112
113/// Used to indicate an unsuccessful operation when none of the other NativeError objects are an appropriate indication of the failure cause.
114// https://heycam.github.io/webidl/#notsupportederror
115#[derive(Clone, Debug, ReferenceType)]
116#[reference(subclass_of(Error, DomException))]
117pub struct NotSupportedError( Reference );
118
119impl IError for NotSupportedError {}
120impl IDomException for NotSupportedError {}
121
122error_boilerplate! { NotSupportedError, dom_exception = "NotSupportedError" }
123
124/// Used to indicate the string contains one or more characters which are invalid.
125// https://heycam.github.io/webidl/#invalidcharactererror
126#[derive(Clone, Debug, ReferenceType)]
127#[reference(subclass_of(Error, DomException))]
128pub struct InvalidCharacterError( Reference );
129
130impl IError for InvalidCharacterError {}
131impl IDomException for InvalidCharacterError {}
132
133error_boilerplate! { InvalidCharacterError, dom_exception = "InvalidCharacterError" }
134
135/// Used to indicate that a pointer id passed as an argument was for some reason invalid.
136// https://w3c.github.io/pointerevents/#extensions-to-the-element-interface
137#[derive(Clone, Debug, ReferenceType)]
138#[reference(subclass_of(Error, DomException))]
139pub struct InvalidPointerId( Reference );
140
141impl IError for InvalidPointerId {}
142impl IDomException for InvalidPointerId {}
143
144error_boilerplate! { InvalidPointerId, dom_exception = "InvalidPointerId" }
145
146/// Used to indicate that the operation was aborted.
147// https://heycam.github.io/webidl/#aborterror
148#[derive(Clone, Debug, ReferenceType)]
149#[reference(subclass_of(Error, DomException))]
150pub struct AbortError( Reference );
151
152impl IError for AbortError {}
153impl IDomException for AbortError {}
154
155error_boilerplate! { AbortError, dom_exception = "AbortError" }
156
157/// Indicates an xml namespace-related feature was used incorrectly.
158// https://heycam.github.io/webidl/#namespaceerror
159#[derive(Clone, Debug, ReferenceType)]
160#[reference(subclass_of(Error, DomException))]
161pub struct NamespaceError( Reference );
162
163impl IError for NamespaceError {}
164impl IDomException for NamespaceError {}
165
166error_boilerplate! { NamespaceError, dom_exception = "NamespaceError" }
167
168#[cfg(all(test, feature = "web_test"))]
169mod test {
170    use super::*;
171    use webcore::try_from::TryInto;
172
173    fn new_dom_exception(message: &str, name: &str) -> DomException {
174        js!(
175            return new DOMException(@{message}, @{name});
176        ).try_into().unwrap()
177    }
178
179    #[test]
180    fn test_error() {
181        let name = "HierarchyRequestError";
182        // Successful downcast.
183        let err: DomException = new_dom_exception("foo", name);
184        let err: HierarchyRequestError = err.try_into().expect("Expected HierarchyRequestError");
185        assert_eq!(err.name(), name);
186
187        // Failed downcast.
188        let err: DomException = new_dom_exception("foo", name);
189        let err: Result<SyntaxError, _> = err.try_into();
190        assert!(err.is_err());
191    }
192}