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("Invalid address length: Ethereum addresses must be at least 20 bytes")]
34    InvalidAddressLength,
35
36    #[error("Invalid Ethereum address format: {0}")]
37    InvalidEthAddress(String),
38
39    #[error("Failed to parse hex: {0}")]
40    HexError(#[from] hex::FromHexError),
41}
42
43#[derive(Error, Debug, PartialEq)]
44pub enum MathError {
45    #[error("division by zero not well defined")]
46    DivisionByZero,
47
48    #[error("could not parse decimal from string \"{}\": {}", dec_str, err)]
49    CwDecParseError { dec_str: String, err: cw::StdError },
50
51    #[error("could not parse to cosmosdk.io/math.LegacyDec: {0}")]
52    SdkDecError(String),
53}
54
55impl From<NibiruError> for cw::StdError {
56    fn from(err: NibiruError) -> cw::StdError {
57        match err {
58            NibiruError::CwStd(e) => e,
59            e => cw::StdError::generic_err(e.to_string()),
60        }
61    }
62}