ethers_abi/
errors.rs

1// Copyright 2015-2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use crate::no_std_prelude::Cow;
10#[cfg(not(feature = "std"))]
11use crate::no_std_prelude::*;
12#[cfg(feature = "serde")]
13use core::num;
14#[cfg(feature = "std")]
15use thiserror::Error;
16
17/// Ethabi result type
18pub type Result<T> = core::result::Result<T, Error>;
19
20/// Ethabi errors
21#[cfg_attr(feature = "std", derive(Error))]
22#[derive(Debug)]
23pub enum Error {
24    /// Invalid entity such as a bad function name.
25    #[cfg_attr(feature = "std", error("Invalid name: {0}"))]
26    InvalidName(String),
27    /// Invalid data.
28    #[cfg_attr(feature = "std", error("Invalid data"))]
29    InvalidData,
30    /// Serialization error.
31    #[cfg(feature = "full-serde")]
32    #[error("Serialization error: {0}")]
33    SerdeJson(#[from] serde_json::Error),
34    /// Integer parsing error.
35    #[cfg(feature = "serde")]
36    #[cfg_attr(feature = "std", error("Integer parsing error: {0}"))]
37    ParseInt(#[cfg_attr(feature = "std", from)] num::ParseIntError),
38    /// Hex string parsing error.
39    #[cfg(feature = "serde")]
40    #[cfg_attr(feature = "std", error("Hex parsing error: {0}"))]
41    Hex(#[cfg_attr(feature = "std", from)] hex::FromHexError),
42    /// Other errors.
43    #[cfg_attr(feature = "std", error("{0}"))]
44    Other(Cow<'static, str>),
45}
46
47#[cfg(feature = "serde")]
48impl From<ethereum_types::FromDecStrErr> for Error {
49    fn from(err: ethereum_types::FromDecStrErr) -> Self {
50        use ethereum_types::FromDecStrErr::*;
51        match err {
52            InvalidCharacter => Self::Other(Cow::Borrowed("Uint parse error: InvalidCharacter")),
53            InvalidLength => Self::Other(Cow::Borrowed("Uint parse error: InvalidLength")),
54        }
55    }
56}