aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/bindings/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/script/dom/bindings/error.rs')
-rw-r--r--src/components/script/dom/bindings/error.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/components/script/dom/bindings/error.rs b/src/components/script/dom/bindings/error.rs
index cef1de163fd..aa0642179a3 100644
--- a/src/components/script/dom/bindings/error.rs
+++ b/src/components/script/dom/bindings/error.rs
@@ -2,6 +2,8 @@
* 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/. */
+//! Utilities to throw exceptions from Rust bindings.
+
use dom::bindings::conversions::ToJSValConvertible;
use dom::bindings::global::GlobalRef;
use dom::domexception::DOMException;
@@ -16,6 +18,7 @@ use js::rust::with_compartment;
use libc;
use std::ptr;
+/// DOM exceptions that can be thrown by a native DOM method.
#[deriving(Show)]
pub enum Error {
IndexSize,
@@ -34,10 +37,14 @@ pub enum Error {
Timeout
}
+/// The return type for IDL operations that can throw DOM exceptions.
pub type Fallible<T> = Result<T, Error>;
+/// The return type for IDL operations that can throw DOM exceptions and
+/// return `()`.
pub type ErrorResult = Fallible<()>;
+/// Set a pending DOM exception for the given `result` on `cx`.
pub fn throw_dom_exception(cx: *mut JSContext, global: &GlobalRef,
result: Error) {
assert!(unsafe { JS_IsExceptionPending(cx) } == 0);
@@ -48,6 +55,7 @@ pub fn throw_dom_exception(cx: *mut JSContext, global: &GlobalRef,
}
}
+/// Report a pending exception, thereby clearing it.
pub fn report_pending_exception(cx: *mut JSContext, obj: *mut JSObject) {
unsafe {
if JS_IsExceptionPending(cx) != 0 {
@@ -62,6 +70,8 @@ pub fn report_pending_exception(cx: *mut JSContext, obj: *mut JSObject) {
}
}
+/// Throw an exception to signal that a `JSVal` can not be converted to any of
+/// the types in an IDL union type.
pub fn throw_not_in_union(cx: *mut JSContext, names: &'static str) -> JSBool {
assert!(unsafe { JS_IsExceptionPending(cx) } == 0);
let message = format!("argument could not be converted to any of: {}", names);
@@ -71,6 +81,7 @@ pub fn throw_not_in_union(cx: *mut JSContext, names: &'static str) -> JSBool {
return 0;
}
+/// Format string used to throw `TypeError`s.
static ERROR_FORMAT_STRING_STRING: [libc::c_char, ..4] = [
'{' as libc::c_char,
'0' as libc::c_char,
@@ -78,12 +89,14 @@ static ERROR_FORMAT_STRING_STRING: [libc::c_char, ..4] = [
0 as libc::c_char,
];
+/// Format string struct used to throw `TypeError`s.
static ERROR_FORMAT_STRING: JSErrorFormatString = JSErrorFormatString {
format: &ERROR_FORMAT_STRING_STRING as *libc::c_char,
argCount: 1,
exnType: JSEXN_TYPEERR as i16,
};
+/// Callback used to throw `TypeError`s.
extern fn get_error_message(_user_ref: *mut libc::c_void,
_locale: *libc::c_char,
error_number: libc::c_uint) -> *JSErrorFormatString
@@ -92,6 +105,7 @@ extern fn get_error_message(_user_ref: *mut libc::c_void,
&ERROR_FORMAT_STRING as *JSErrorFormatString
}
+/// Throw a `TypeError` with the given message.
pub fn throw_type_error(cx: *mut JSContext, error: &str) {
let error = error.to_c_str();
error.with_ref(|error| unsafe {