Skip to main content

zing_cli/
error.rs

1use std::process::ExitCode;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5#[allow(dead_code)]
6pub enum ZingError {
7    #[error("Config error: {0}")]
8    Config(String),
9
10    #[error("Keystore error: {0}")]
11    Keystore(String),
12
13    #[error("Insufficient USDC balance. Need at least 0.01 USDC")]
14    InsufficientBalance,
15
16    #[error("Transaction failed: {0}")]
17    Transaction(String),
18
19    #[error("API error ({status}): {body}")]
20    Api { status: u16, body: String },
21
22    #[error("Network error: {0}")]
23    Network(String),
24
25    #[error("{0}")]
26    Generic(String),
27}
28
29impl ZingError {
30    #[allow(dead_code)]
31    pub fn exit_code(&self) -> ExitCode {
32        match self {
33            Self::Config(_) | Self::Keystore(_) | Self::InsufficientBalance | Self::Transaction(_) => {
34                ExitCode::from(1)
35            }
36            Self::Api { .. } => ExitCode::from(2),
37            Self::Network(_) => ExitCode::from(3),
38            Self::Generic(_) => ExitCode::from(1),
39        }
40    }
41}
42
43impl From<anyhow::Error> for ZingError {
44    fn from(e: anyhow::Error) -> Self {
45        Self::Generic(e.to_string())
46    }
47}