pub struct Error(/* private fields */);
Expand description
Wrapper type for Ruby Exception
s or other interrupts.
Implementations§
source§impl Error
impl Error
sourcepub fn new<T>(class: ExceptionClass, msg: T) -> Self
pub fn new<T>(class: ExceptionClass, msg: T) -> Self
Create a new Error
that can be raised as a Ruby Exception
with
msg
.
§Examples
use magnus::{define_global_function, eval, exception, function, prelude::*, Error, Exception};
fn bang() -> Result<(), Error> {
Err(Error::new(exception::runtime_error(), "BANG"))
}
define_global_function("bang", function!(bang, 0));
let error: Exception = eval(
"
begin
bang
rescue => e
e
end
",
)
.unwrap();
assert!(error.is_kind_of(exception::runtime_error()));
let msg: String = error.funcall("message", ()).unwrap();
assert_eq!(msg, "BANG")
sourcepub fn iter_break<T>(val: T) -> Selfwhere
T: IntoValue,
pub fn iter_break<T>(val: T) -> Selfwhere
T: IntoValue,
Create a new error that will break from a loop when returned to Ruby.
§Panics
Panics if called from a non-Ruby thread. See Ruby::iter_break_value
for the non-panicking version.
§Examples
use magnus::{prelude::*, Error};
let i: i64 = magnus::Range::new(1, 100, false)
.unwrap()
.block_call("each", (), |args, _block| {
let i = i64::try_convert(*args.get(0).unwrap())?;
if i % 3 == 0 && i % 5 == 0 {
Err(Error::iter_break(i))
} else {
Ok(())
}
})
.unwrap();
assert_eq!(i, 15);
sourcepub fn is_kind_of<T>(&self, class: T) -> bool
pub fn is_kind_of<T>(&self, class: T) -> bool
Matches the internal Exception
against class
with same semantics as
Ruby’s rescue
.
§Examples
use magnus::{
class, eval, exception::ExceptionClass, prelude::*, Error, RModule, TryConvert, Value,
};
let err: Error = eval::<Value>(
"
class ExampleError < StandardError
end
module Tag
end
class SpecificError < ExampleError
include Tag
end
raise SpecificError
",
)
.unwrap_err();
fn get<T: TryConvert>(name: &str) -> T {
class::object().const_get::<_, T>(name).unwrap()
}
assert!(err.is_kind_of(get::<ExceptionClass>("SpecificError")));
assert!(err.is_kind_of(get::<ExceptionClass>("ExampleError")));
assert!(err.is_kind_of(get::<ExceptionClass>("StandardError")));
assert!(err.is_kind_of(get::<ExceptionClass>("Exception")));
assert!(err.is_kind_of(get::<RModule>("Tag")));
assert!(!err.is_kind_of(get::<ExceptionClass>("NoMethodError")));
assert!(!err.is_kind_of(get::<RModule>("Math")));
sourcepub fn error_type(&self) -> &ErrorType
pub fn error_type(&self) -> &ErrorType
Returns the ErrorType
for self.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Error
impl RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnwindSafe for Error
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more