File: return_custom_error.rs

package info (click to toggle)
rust-magnus 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,468 kB
  • sloc: ruby: 150; sh: 17; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 670 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use magnus::{error::IntoError, function, rb_assert, Error, Ruby};

struct CustomError(&'static str);

impl IntoError for CustomError {
    fn into_error(self, ruby: &Ruby) -> Error {
        Error::new(
            ruby.exception_runtime_error(),
            format!("Custom error: {}", self.0),
        )
    }
}

fn example() -> Result<(), CustomError> {
    Err(CustomError("test"))
}

#[test]
fn it_can_bind_function_returning_custom_error() {
    let ruby = unsafe { magnus::embed::init() };

    ruby.define_global_function("example", function!(example, 0));

    rb_assert!(
        ruby,
        r#"(example rescue $!).message == "Custom error: test""#
    );
}