js_error

Macro js_error 

Source
macro_rules! js_error {
    (Error: $value: literal) => { ... };
    (Error: $value: literal $(, $args: expr)* $(,)?) => { ... };
    (TypeError: $value: literal) => { ... };
    (TypeError: $value: literal $(, $args: expr)* $(,)?) => { ... };
    (SyntaxError: $value: literal) => { ... };
    (SyntaxError: $value: literal $(, $args: expr)* $(,)?) => { ... };
    (RangeError: $value: literal) => { ... };
    (RangeError: $value: literal $(, $args: expr)* $(,)?) => { ... };
    (EvalError: $value: literal) => { ... };
    (EvalError: $value: literal $(, $args: expr)* $(,)?) => { ... };
    (ReferenceError: $value: literal) => { ... };
    (ReferenceError: $value: literal $(, $args: expr)* $(,)?) => { ... };
    (URIError: $value: literal) => { ... };
    (URIError: $value: literal $(, $args: expr)* $(,)?) => { ... };
    ($value: literal) => { ... };
    ($value: expr) => { ... };
    ($value: literal $(, $args: expr)* $(,)?) => { ... };
}
Expand description

Create an error object from a value or string literal. Optionally the first argument of the macro can be a type of error (such as TypeError, RangeError or InternalError).

Can be used with an expression that converts into JsValue or a format string with arguments.

§Native Errors

The only native error that is not buildable using this macro is AggregateError, which requires multiple error objects available at construction.

InternalError is non-standard and unsupported in Boa.

All other native error types can be created from the macro using their JavaScript name followed by a colon, like:

js_error!(TypeError: "hello world");

§Examples

use boa_engine::js_error;
let context = &mut Context::default();

let error = js_error!("error!");
assert!(error.as_opaque().is_some());
assert_eq!(
    error.as_opaque().unwrap().to_string(context).unwrap(),
    "error!"
);

let error = js_error!("error: {}", 5);
assert_eq!(
    error.as_opaque().unwrap().to_string(context).unwrap(),
    "error: 5"
);

// Non-string literals must be used as an expression.
let error = js_error!({ true });
assert_eq!(error.as_opaque().unwrap(), &JsValue::from(true));