multiversx_sc/api/
error_api.rs

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use super::HandleTypeInfo;

pub trait ErrorApi: HandleTypeInfo {
    type ErrorApiImpl: ErrorApiImpl
        + HandleTypeInfo<
            ManagedBufferHandle = Self::ManagedBufferHandle,
            BigIntHandle = Self::BigIntHandle,
            BigFloatHandle = Self::BigFloatHandle,
            EllipticCurveHandle = Self::EllipticCurveHandle,
        >;

    fn error_api_impl() -> Self::ErrorApiImpl;
}

pub trait ErrorApiImpl: HandleTypeInfo {
    fn signal_error(&self, message: &[u8]) -> !;

    fn signal_error_from_buffer(&self, message_handle: Self::ManagedBufferHandle) -> !;
}

/// An error handler that simply panics whenever `signal_error` is called.
/// Especially useful for unit tests.
/// Implements `ErrorApi`.
pub struct PanickingErrorApiImpl;

impl ErrorApiImpl for PanickingErrorApiImpl {
    fn signal_error(&self, message: &[u8]) -> ! {
        panic!(
            "PanickingErrorApi panicked: {}",
            core::str::from_utf8(message).unwrap()
        )
    }

    fn signal_error_from_buffer(&self, _message_handle: Self::ManagedBufferHandle) -> ! {
        panic!("PanickingErrorApi panicked via signal_error_from_buffer")
    }
}

impl HandleTypeInfo for PanickingErrorApiImpl {
    type ManagedBufferHandle = i32;

    type BigIntHandle = i32;

    type BigFloatHandle = i32;

    type EllipticCurveHandle = i32;

    type ManagedMapHandle = i32;
}

/// An error handler that simply panics whenever `signal_error` is called.
/// Especially useful for unit tests.
/// Implements `ErrorApi`.
pub struct PanickingErrorApi;

impl ErrorApi for PanickingErrorApi {
    type ErrorApiImpl = PanickingErrorApiImpl;

    fn error_api_impl() -> Self::ErrorApiImpl {
        PanickingErrorApiImpl
    }
}

impl HandleTypeInfo for PanickingErrorApi {
    type ManagedBufferHandle = i32;

    type BigIntHandle = i32;

    type BigFloatHandle = i32;

    type EllipticCurveHandle = i32;

    type ManagedMapHandle = i32;
}