1 use webcore::value::Reference; 2 use 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 9 pub 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))] 18 pub struct DomException( Reference ); 19 20 impl IError for DomException {} 21 impl IDomException for DomException {} 22 23 error_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))] 29 pub struct HierarchyRequestError( Reference ); 30 31 impl IError for HierarchyRequestError {} 32 impl IDomException for HierarchyRequestError {} 33 34 error_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))] 40 pub struct InvalidAccessError( Reference ); 41 42 impl IError for InvalidAccessError {} 43 impl IDomException for InvalidAccessError {} 44 45 error_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))] 51 pub struct NoModificationAllowedError( Reference ); 52 53 impl IError for NoModificationAllowedError {} 54 impl IDomException for NoModificationAllowedError {} 55 56 error_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))] 62 pub struct NotFoundError( Reference ); 63 64 impl IError for NotFoundError {} 65 impl IDomException for NotFoundError {} 66 67 error_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))] 73 pub struct SecurityError( Reference ); 74 75 impl IError for SecurityError {} 76 impl IDomException for SecurityError {} 77 78 error_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))] 84 pub struct SyntaxError( Reference ); 85 86 impl IError for SyntaxError {} 87 impl IDomException for SyntaxError {} 88 89 error_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))] 95 pub struct IndexSizeError( Reference ); 96 97 impl IError for IndexSizeError {} 98 impl IDomException for IndexSizeError {} 99 100 error_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))] 106 pub struct InvalidStateError( Reference ); 107 108 impl IError for InvalidStateError {} 109 impl IDomException for InvalidStateError {} 110 111 error_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))] 117 pub struct NotSupportedError( Reference ); 118 119 impl IError for NotSupportedError {} 120 impl IDomException for NotSupportedError {} 121 122 error_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))] 128 pub struct InvalidCharacterError( Reference ); 129 130 impl IError for InvalidCharacterError {} 131 impl IDomException for InvalidCharacterError {} 132 133 error_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))] 139 pub struct InvalidPointerId( Reference ); 140 141 impl IError for InvalidPointerId {} 142 impl IDomException for InvalidPointerId {} 143 144 error_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))] 150 pub struct AbortError( Reference ); 151 152 impl IError for AbortError {} 153 impl IDomException for AbortError {} 154 155 error_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))] 161 pub struct NamespaceError( Reference ); 162 163 impl IError for NamespaceError {} 164 impl IDomException for NamespaceError {} 165 166 error_boilerplate! { NamespaceError, dom_exception = "NamespaceError" } 167 168 #[cfg(all(test, feature = "web_test"))] 169 mod test { 170 use super::*; 171 use webcore::try_from::TryInto; 172 new_dom_exception(message: &str, name: &str) -> DomException173 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] test_error()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 } 193