stylus_core/calls/
errors.rs

1// Copyright 2025-2026, Offchain Labs, Inc.
2// For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/main/licenses/COPYRIGHT.md
3//
4extern crate alloc;
5
6use alloc::vec::Vec;
7use alloy_sol_types::{Panic, PanicKind, SolError};
8
9/// Represents error data when a call fails.
10#[derive(Debug, PartialEq)]
11pub enum Error {
12    /// Revert data returned by the other contract.
13    Revert(Vec<u8>),
14    /// Failure to decode the other contract's return data.
15    AbiDecodingFailed(alloy_sol_types::Error),
16}
17
18impl From<alloy_sol_types::Error> for Error {
19    fn from(err: alloy_sol_types::Error) -> Self {
20        Error::AbiDecodingFailed(err)
21    }
22}
23
24/// Encode an error.
25///
26/// This is useful so that users can use `Error` as a variant in their error
27/// types. It should not be necessary to implement this.
28pub trait MethodError {
29    /// Users should not have to call this.
30    fn encode(self) -> Vec<u8>;
31}
32
33impl MethodError for Error {
34    #[inline]
35    fn encode(self) -> Vec<u8> {
36        From::from(self)
37    }
38}
39
40impl<T: SolError> MethodError for T {
41    #[inline]
42    fn encode(self) -> Vec<u8> {
43        SolError::abi_encode(&self)
44    }
45}
46
47impl From<Error> for Vec<u8> {
48    fn from(err: Error) -> Vec<u8> {
49        match err {
50            Error::Revert(data) => data,
51            #[allow(unused)]
52            Error::AbiDecodingFailed(err) => Panic::from(PanicKind::Generic).abi_encode(),
53        }
54    }
55}