Struct magnus::error::Error

source ·
pub struct Error(/* private fields */);
Expand description

Wrapper type for Ruby Exceptions or other interrupts.

Implementations§

source§

impl Error

source

pub fn new<T>(class: ExceptionClass, msg: T) -> Self
where T: Into<Cow<'static, str>>,

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")
source

pub fn iter_break<T>(val: T) -> Self
where 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);
source

pub fn is_kind_of<T>(&self, class: T) -> bool
where T: ReprValue + Module,

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")));
source

pub fn error_type(&self) -> &ErrorType

Returns the ErrorType for self.

source

pub fn value(&self) -> Option<Value>

Returns the inner Value of self, if there is one.

The returned Value may be a subclass or an instance of Exception.

This function is provided for rare cases where the Error needs to be stored on the heap and the inner value needs to be marked to avoid being garbage collected.

Trait Implementations§

source§

impl Debug for Error

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Error

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<Error> for OpaqueError

source§

fn from(err: Error) -> Self

Converts to this type from the input type.
source§

impl From<Exception> for Error

source§

fn from(val: Exception) -> Self

Converts to this type from the input type.

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Inspect for T
where T: Debug,

source§

fn inspect(&self) -> String

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.