diff options
Diffstat (limited to 'components/script/dom/domexception.rs')
-rw-r--r-- | components/script/dom/domexception.rs | 161 |
1 files changed, 115 insertions, 46 deletions
diff --git a/components/script/dom/domexception.rs b/components/script/dom/domexception.rs index 628ae5ceed5..c2533cd7932 100644 --- a/components/script/dom/domexception.rs +++ b/components/script/dom/domexception.rs @@ -1,18 +1,18 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use dom::bindings::codegen::Bindings::DOMExceptionBinding; -use dom::bindings::codegen::Bindings::DOMExceptionBinding::DOMExceptionConstants; -use dom::bindings::codegen::Bindings::DOMExceptionBinding::DOMExceptionMethods; -use dom::bindings::js::Root; -use dom::bindings::reflector::{Reflector, reflect_dom_object}; -use dom::bindings::str::DOMString; -use dom::globalscope::GlobalScope; +use crate::dom::bindings::codegen::Bindings::DOMExceptionBinding::DOMExceptionConstants; +use crate::dom::bindings::codegen::Bindings::DOMExceptionBinding::DOMExceptionMethods; +use crate::dom::bindings::error::Error; +use crate::dom::bindings::reflector::{reflect_dom_object, Reflector}; +use crate::dom::bindings::root::DomRoot; +use crate::dom::bindings::str::DOMString; +use crate::dom::globalscope::GlobalScope; use dom_struct::dom_struct; #[repr(u16)] -#[derive(JSTraceable, Copy, Clone, Debug, HeapSizeOf)] +#[derive(Clone, Copy, Debug, Eq, JSTraceable, MallocSizeOf, Ord, PartialEq, PartialOrd)] pub enum DOMErrorName { IndexSizeError = DOMExceptionConstants::INDEX_SIZE_ERR, HierarchyRequestError = DOMExceptionConstants::HIERARCHY_REQUEST_ERR, @@ -31,49 +31,61 @@ pub enum DOMErrorName { NetworkError = DOMExceptionConstants::NETWORK_ERR, AbortError = DOMExceptionConstants::ABORT_ERR, TypeMismatchError = DOMExceptionConstants::TYPE_MISMATCH_ERR, + URLMismatchError = DOMExceptionConstants::URL_MISMATCH_ERR, QuotaExceededError = DOMExceptionConstants::QUOTA_EXCEEDED_ERR, TimeoutError = DOMExceptionConstants::TIMEOUT_ERR, InvalidNodeTypeError = DOMExceptionConstants::INVALID_NODE_TYPE_ERR, DataCloneError = DOMExceptionConstants::DATA_CLONE_ERR, + NotReadableError, + OperationError, +} + +impl DOMErrorName { + pub fn from(s: &DOMString) -> Option<DOMErrorName> { + match s.as_ref() { + "IndexSizeError" => Some(DOMErrorName::IndexSizeError), + "HierarchyRequestError" => Some(DOMErrorName::HierarchyRequestError), + "WrongDocumentError" => Some(DOMErrorName::WrongDocumentError), + "InvalidCharacterError" => Some(DOMErrorName::InvalidCharacterError), + "NoModificationAllowedError" => Some(DOMErrorName::NoModificationAllowedError), + "NotFoundError" => Some(DOMErrorName::NotFoundError), + "NotSupportedError" => Some(DOMErrorName::NotSupportedError), + "InUseAttributeError" => Some(DOMErrorName::InUseAttributeError), + "InvalidStateError" => Some(DOMErrorName::InvalidStateError), + "SyntaxError" => Some(DOMErrorName::SyntaxError), + "InvalidModificationError" => Some(DOMErrorName::InvalidModificationError), + "NamespaceError" => Some(DOMErrorName::NamespaceError), + "InvalidAccessError" => Some(DOMErrorName::InvalidAccessError), + "SecurityError" => Some(DOMErrorName::SecurityError), + "NetworkError" => Some(DOMErrorName::NetworkError), + "AbortError" => Some(DOMErrorName::AbortError), + "TypeMismatchError" => Some(DOMErrorName::TypeMismatchError), + "URLMismatchError" => Some(DOMErrorName::URLMismatchError), + "QuotaExceededError" => Some(DOMErrorName::QuotaExceededError), + "TimeoutError" => Some(DOMErrorName::TimeoutError), + "InvalidNodeTypeError" => Some(DOMErrorName::InvalidNodeTypeError), + "DataCloneError" => Some(DOMErrorName::DataCloneError), + "NotReadableError" => Some(DOMErrorName::NotReadableError), + "OperationError" => Some(DOMErrorName::OperationError), + _ => None, + } + } } #[dom_struct] pub struct DOMException { reflector_: Reflector, - code: DOMErrorName, + message: DOMString, + name: DOMString, } impl DOMException { - fn new_inherited(code: DOMErrorName) -> DOMException { - DOMException { - reflector_: Reflector::new(), - code: code, - } - } - - pub fn new(global: &GlobalScope, code: DOMErrorName) -> Root<DOMException> { - reflect_dom_object(box DOMException::new_inherited(code), - global, - DOMExceptionBinding::Wrap) - } -} - -impl DOMExceptionMethods for DOMException { - // https://heycam.github.io/webidl/#dfn-DOMException - fn Code(&self) -> u16 { - self.code as u16 - } - - // https://heycam.github.io/webidl/#idl-DOMException-error-names - fn Name(&self) -> DOMString { - DOMString::from(format!("{:?}", self.code)) - } - - // https://heycam.github.io/webidl/#error-names - fn Message(&self) -> DOMString { - let message = match self.code { + fn get_error_data_by_code(code: DOMErrorName) -> (DOMString, DOMString) { + let message = match &code { DOMErrorName::IndexSizeError => "The index is not in the allowed range.", - DOMErrorName::HierarchyRequestError => "The operation would yield an incorrect node tree.", + DOMErrorName::HierarchyRequestError => { + "The operation would yield an incorrect node tree." + }, DOMErrorName::WrongDocumentError => "The object is in the wrong document.", DOMErrorName::InvalidCharacterError => "The string contains invalid characters.", DOMErrorName::NoModificationAllowedError => "The object can not be modified.", @@ -84,23 +96,80 @@ impl DOMExceptionMethods for DOMException { DOMErrorName::SyntaxError => "The string did not match the expected pattern.", DOMErrorName::InvalidModificationError => "The object can not be modified in this way.", DOMErrorName::NamespaceError => "The operation is not allowed by Namespaces in XML.", - DOMErrorName::InvalidAccessError => "The object does not support the operation or argument.", + DOMErrorName::InvalidAccessError => { + "The object does not support the operation or argument." + }, DOMErrorName::SecurityError => "The operation is insecure.", DOMErrorName::NetworkError => "A network error occurred.", DOMErrorName::AbortError => "The operation was aborted.", DOMErrorName::TypeMismatchError => "The given type does not match any expected type.", + DOMErrorName::URLMismatchError => "The given URL does not match another URL.", DOMErrorName::QuotaExceededError => "The quota has been exceeded.", DOMErrorName::TimeoutError => "The operation timed out.", - DOMErrorName::InvalidNodeTypeError => - "The supplied node is incorrect or has an incorrect ancestor for this operation.", + DOMErrorName::InvalidNodeTypeError => { + "The supplied node is incorrect or has an incorrect ancestor for this operation." + }, DOMErrorName::DataCloneError => "The object can not be cloned.", + DOMErrorName::NotReadableError => "The I/O read operation failed.", + DOMErrorName::OperationError => { + "The operation failed for an operation-specific reason." + }, }; - DOMString::from(message) + ( + DOMString::from(message), + DOMString::from(format!("{:?}", code)), + ) + } + + fn new_inherited(message_: DOMString, name_: DOMString) -> DOMException { + DOMException { + reflector_: Reflector::new(), + message: message_, + name: name_, + } + } + + pub fn new(global: &GlobalScope, code: DOMErrorName) -> DomRoot<DOMException> { + let (message, name) = DOMException::get_error_data_by_code(code); + + reflect_dom_object(Box::new(DOMException::new_inherited(message, name)), global) + } + + #[allow(non_snake_case)] + pub fn Constructor( + global: &GlobalScope, + message: DOMString, + name: DOMString, + ) -> Result<DomRoot<DOMException>, Error> { + Ok(reflect_dom_object( + Box::new(DOMException::new_inherited(message, name)), + global, + )) } - // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-error.prototype.tostring - fn Stringifier(&self) -> DOMString { - DOMString::from(format!("{}: {}", self.Name(), self.Message())) + // not an IDL stringifier, used internally + pub fn stringifier(&self) -> DOMString { + DOMString::from(format!("{}: {}", self.name, self.message)) + } +} + +impl DOMExceptionMethods for DOMException { + // https://heycam.github.io/webidl/#dom-domexception-code + fn Code(&self) -> u16 { + match DOMErrorName::from(&self.name) { + Some(code) if code <= DOMErrorName::DataCloneError => code as u16, + _ => 0, + } + } + + // https://heycam.github.io/webidl/#idl-DOMException-error-names + fn Name(&self) -> DOMString { + self.name.clone() + } + + // https://heycam.github.io/webidl/#error-names + fn Message(&self) -> DOMString { + self.message.clone() } } |