Skip to main content

error

Macro error 

Source
macro_rules! error {
    (class = $class:expr, $($arg:tt)*) => { ... };
    ($($arg:tt)*) => { ... };
}
Expand description

Raise an R error from Rust with rust_error class layering.

Requires error_in_r mode (the default for #[miniextendr] functions). The raised condition has class c("rust_error", "simpleError", "error", "condition").

An optional class = "name" form prepends a custom class for programmatic catching: c("name", "rust_error", "simpleError", "error", "condition").

§Examples

use miniextendr_api::error;

#[miniextendr]
fn fail() {
    error!("something went wrong: {}", 42);
}

// With a custom class for tryCatch:
#[miniextendr]
fn typed_fail(name: &str) {
    error!(class = "my_error", "missing field: {name}");
}
tryCatch(fail(), rust_error = function(e) conditionMessage(e))
# [1] "something went wrong: 42"

tryCatch(typed_fail("x"), my_error = function(e) "caught!")
# [1] "caught!"