multiversx_sc/api/
error_api.rs

1use super::HandleTypeInfo;
2
3pub trait ErrorApi: HandleTypeInfo {
4    type ErrorApiImpl: ErrorApiImpl
5        + HandleTypeInfo<
6            ManagedBufferHandle = Self::ManagedBufferHandle,
7            BigIntHandle = Self::BigIntHandle,
8            BigFloatHandle = Self::BigFloatHandle,
9            EllipticCurveHandle = Self::EllipticCurveHandle,
10        >;
11
12    fn error_api_impl() -> Self::ErrorApiImpl;
13}
14
15pub trait ErrorApiImpl: HandleTypeInfo {
16    fn signal_error(&self, message: &[u8]) -> !;
17
18    fn signal_error_from_buffer(&self, message_handle: Self::ManagedBufferHandle) -> !;
19}
20
21/// An error handler that simply panics whenever `signal_error` is called.
22/// Especially useful for unit tests.
23/// Implements `ErrorApi`.
24pub struct PanickingErrorApiImpl;
25
26impl ErrorApiImpl for PanickingErrorApiImpl {
27    fn signal_error(&self, message: &[u8]) -> ! {
28        panic!(
29            "PanickingErrorApi panicked: {}",
30            core::str::from_utf8(message).unwrap()
31        )
32    }
33
34    fn signal_error_from_buffer(&self, _message_handle: Self::ManagedBufferHandle) -> ! {
35        panic!("PanickingErrorApi panicked via signal_error_from_buffer")
36    }
37}
38
39impl HandleTypeInfo for PanickingErrorApiImpl {
40    type ManagedBufferHandle = i32;
41
42    type BigIntHandle = i32;
43
44    type BigFloatHandle = i32;
45
46    type EllipticCurveHandle = i32;
47
48    type ManagedMapHandle = i32;
49}
50
51/// An error handler that simply panics whenever `signal_error` is called.
52/// Especially useful for unit tests.
53/// Implements `ErrorApi`.
54pub struct PanickingErrorApi;
55
56impl ErrorApi for PanickingErrorApi {
57    type ErrorApiImpl = PanickingErrorApiImpl;
58
59    fn error_api_impl() -> Self::ErrorApiImpl {
60        PanickingErrorApiImpl
61    }
62}
63
64impl HandleTypeInfo for PanickingErrorApi {
65    type ManagedBufferHandle = i32;
66
67    type BigIntHandle = i32;
68
69    type BigFloatHandle = i32;
70
71    type EllipticCurveHandle = i32;
72
73    type ManagedMapHandle = i32;
74}