Skip to main content

cow_rs/
error.rs

1//! Error type for the `CoW` Protocol SDK.
2//!
3//! [`CowError`] is the unified error type used across all modules.
4//! Every fallible function in the SDK returns `Result<T, CowError>`.
5//!
6//! # Variants
7//!
8//! | Variant | When |
9//! |---|---|
10//! | [`UnknownAsset`](CowError::UnknownAsset) | Asset symbol not in [`TokenRegistry`](crate::config::TokenRegistry) |
11//! | [`Api`](CowError::Api) | Orderbook/subgraph returned non-2xx |
12//! | [`Http`](CowError::Http) | Network transport failure |
13//! | [`Signing`](CowError::Signing) | ECDSA / EIP-712 signing failure |
14//! | [`Parse`](CowError::Parse) | Field parsing / deserialisation error |
15//! | [`AppData`](CowError::AppData) | App-data encoding / hashing failure |
16//! | [`Rpc`](CowError::Rpc) | JSON-RPC error from an Ethereum node |
17//! | [`Unsupported`](CowError::Unsupported) | Feature not available on chain/config |
18//! | [`Config`](CowError::Config) | SDK configuration error |
19//! | [`ZeroQuantity`](CowError::ZeroQuantity) | Trade amount is zero |
20
21/// Errors that can occur when interacting with the `CoW` Protocol SDK.
22///
23/// This is the unified error type returned by every fallible function in
24/// the crate. Each variant carries enough context to produce a useful
25/// diagnostic message via its [`Display`](std::fmt::Display)
26/// implementation.
27#[derive(Debug, thiserror::Error)]
28pub enum CowError {
29    /// The asset symbol is not in the [`TokenRegistry`](crate::config::TokenRegistry).
30    #[error("unknown asset: {0}")]
31    UnknownAsset(String),
32
33    /// The `CoW` Protocol API returned a non-2xx response.
34    #[error("cow api error {status}: {body}")]
35    Api {
36        /// HTTP status code.
37        status: u16,
38        /// Response body text.
39        body: String,
40    },
41
42    /// An HTTP transport error from `reqwest`.
43    #[error("http error: {0}")]
44    Http(#[from] reqwest::Error),
45
46    /// EIP-712 signing failed.
47    #[error("signing error: {0}")]
48    Signing(String),
49
50    /// The signal quantity is zero — nothing to trade.
51    #[error("signal quantity is zero")]
52    ZeroQuantity,
53
54    /// A required field in a quote or order response could not be parsed.
55    #[error("parse error for field '{field}': {reason}")]
56    Parse {
57        /// Field name that failed to parse.
58        field: &'static str,
59        /// Reason for the parse failure.
60        reason: String,
61    },
62
63    /// App-data encoding or hashing failed.
64    #[error("app-data error: {0}")]
65    AppData(String),
66
67    /// A JSON-RPC error returned by an Ethereum node.
68    #[error("rpc error {code}: {message}")]
69    Rpc {
70        /// JSON-RPC error code (e.g., `-32602` for invalid params).
71        code: i64,
72        /// Human-readable error description from the node.
73        message: String,
74    },
75
76    /// A feature or provider is not supported on the current chain or configuration.
77    #[error("unsupported: {message}")]
78    Unsupported {
79        /// Human-readable description of what is not supported.
80        message: String,
81    },
82
83    /// SDK configuration error (e.g. missing global adapter).
84    #[error("config error: {0}")]
85    Config(String),
86}