error.rs 1.13 KB
Newer Older
1
#![feature(panic_info_message)]
2 3 4 5 6 7 8
#![allow(unused_imports)]

use std::panic;

#[macro_use]
extern crate tvm_frontend as tvm;

9
use tvm::{errors::Error, *};
10 11 12

fn main() {
    register_global_func! {
13 14 15 16 17
        fn error(_args: &[TVMArgValue]) -> Result<TVMRetValue, Error> {
            Err(errors::TypeMismatchError{
                expected: "i64".to_string(),
                actual: "f64".to_string(),
            }.into())
18 19 20 21
        }
    }

    let mut registered = function::Builder::default();
22
    registered.get_function("error");
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    assert!(registered.func.is_some());
    registered.args(&[10, 20]);

    println!("expected error message is:");
    panic::set_hook(Box::new(|panic_info| {
        if let Some(msg) = panic_info.message() {
            println!("{:?}", msg);
        }
        if let Some(location) = panic_info.location() {
            println!(
                "panic occurred in file '{}' at line {}",
                location.file(),
                location.line()
            );
        } else {
            println!("panic occurred but can't get location information");
        }
    }));

    let _result = registered.invoke();
}