multiversx_sc/contract_base/wrappers/
error_helper.rs1use core::{borrow::Borrow, marker::PhantomData};
2
3use crate::codec::{DecodeError, EncodeError};
4
5use crate::{
6 api::{ErrorApiImpl, ManagedTypeApi},
7 types::{ManagedBuffer, ManagedSCError, ManagedType, heap::BoxedBytes},
8};
9
10#[derive(Default)]
11pub struct ErrorHelper<M: ManagedTypeApi> {
12 _phantom: PhantomData<M>,
13}
14
15impl<M: ManagedTypeApi> ErrorHelper<M> {
16 pub fn new() -> Self {
17 ErrorHelper {
18 _phantom: PhantomData,
19 }
20 }
21
22 pub fn new_error(&self) -> ManagedSCError<M> {
23 ManagedSCError::new_empty()
24 }
25
26 pub fn signal_error_with_message<T>(message: T) -> !
27 where
28 T: IntoSignalError<M>,
29 {
30 message.signal_error_with_message()
31 }
32}
33
34pub trait IntoSignalError<M: ManagedTypeApi> {
36 fn signal_error_with_message(self) -> !;
37}
38
39impl<M: ManagedTypeApi> IntoSignalError<M> for &str {
40 #[inline]
41 fn signal_error_with_message(self) -> ! {
42 M::error_api_impl().signal_error(self.as_bytes())
43 }
44}
45
46impl<M: ManagedTypeApi> IntoSignalError<M> for &[u8] {
47 #[inline]
48 fn signal_error_with_message(self) -> ! {
49 M::error_api_impl().signal_error(self)
50 }
51}
52
53impl<M: ManagedTypeApi> IntoSignalError<M> for BoxedBytes {
54 #[inline]
55 fn signal_error_with_message(self) -> ! {
56 M::error_api_impl().signal_error(self.as_slice())
57 }
58}
59
60impl<M: ManagedTypeApi> IntoSignalError<M> for EncodeError {
61 #[inline]
62 fn signal_error_with_message(self) -> ! {
63 M::error_api_impl().signal_error(self.message_bytes())
64 }
65}
66
67impl<M: ManagedTypeApi> IntoSignalError<M> for DecodeError {
68 #[inline]
69 fn signal_error_with_message(self) -> ! {
70 M::error_api_impl().signal_error(self.message_bytes())
71 }
72}
73
74impl<M, B> IntoSignalError<M> for B
76where
77 M: ManagedTypeApi,
78 B: Borrow<ManagedBuffer<M>>,
79{
80 fn signal_error_with_message(self) -> ! {
81 M::error_api_impl().signal_error_from_buffer(self.borrow().get_handle())
82 }
83}