nibiru_std/
errors.rs

1use thiserror::Error;
2
3use cosmwasm_std as cw;
4
5/// Shorthand for an empty anyhow::Result. Useful for idiomatic tests.
6pub type TestResult = anyhow::Result<()>;
7
8pub type NibiruResult<T> = Result<T, NibiruError>;
9
10#[derive(Error, Debug, PartialEq)]
11pub enum NibiruError {
12    #[error("{0}")]
13    CwStd(#[from] cw::StdError),
14
15    #[error("no prost::Name implementation for type {}, where prost::Name.type_url() is needed.", type_name)]
16    NoTypeUrl { type_name: String },
17
18    #[error("prost::Name::type_url {} does not correspond to a QueryRequest::Stargate path.", type_url)]
19    ProstNameisNotQuery { type_url: String },
20
21    #[error("prost::Name::type_url {} does not correspond to a CosmosMsg::Stargate type_url.", type_url)]
22    ProstNameisNotMsg { type_url: String },
23
24    #[error("{0}")]
25    MathError(#[from] MathError),
26
27    #[error("Invalid bech32 address: {0}")]
28    Bech32Error(#[from] bech32::Error),
29
30    #[error("Invalid bech32 prefix: expected '{expected}', got '{actual}'")]
31    InvalidBech32Prefix { expected: String, actual: String },
32
33    #[error(
34        "Invalid address length: Ethereum addresses must be at least 20 bytes"
35    )]
36    InvalidAddressLength,
37
38    #[error("Invalid Ethereum address format: {0}")]
39    InvalidEthAddress(String),
40
41    #[error("Failed to parse hex: {0}")]
42    HexError(#[from] hex::FromHexError),
43}
44
45#[derive(Error, Debug, PartialEq)]
46pub enum MathError {
47    #[error("division by zero not well defined")]
48    DivisionByZero,
49
50    #[error("could not parse decimal from string \"{}\": {}", dec_str, err)]
51    CwDecParseError { dec_str: String, err: cw::StdError },
52
53    #[error("could not parse to cosmosdk.io/math.LegacyDec: {0}")]
54    SdkDecError(String),
55}
56
57impl From<NibiruError> for cw::StdError {
58    fn from(err: NibiruError) -> cw::StdError {
59        match err {
60            NibiruError::CwStd(e) => e,
61            e => cw::StdError::generic_err(e.to_string()),
62        }
63    }
64}