Skip to main content

ows_pay/
error.rs

1use thiserror::Error;
2
3/// Error codes for programmatic handling.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum PayErrorCode {
6    /// HTTP transport error (DNS, TLS, timeout).
7    HttpTransport,
8    /// Server returned an unexpected HTTP status.
9    HttpStatus,
10    /// Protocol-level error (malformed 402, bad header encoding).
11    ProtocolMalformed,
12    /// Could not detect which payment protocol to use.
13    ProtocolUnknown,
14    /// Wallet not found or inaccessible.
15    WalletNotFound,
16    /// Key decryption or signing failed.
17    SigningFailed,
18    /// No supported chain/network in the payment requirements.
19    UnsupportedChain,
20    /// Discovery API error.
21    DiscoveryFailed,
22    /// Invalid input (e.g. unsupported HTTP method).
23    InvalidInput,
24}
25
26#[derive(Debug, Error)]
27#[error("[{code:?}] {message}")]
28pub struct PayError {
29    pub code: PayErrorCode,
30    pub message: String,
31}
32
33impl PayError {
34    pub fn new(code: PayErrorCode, message: impl Into<String>) -> Self {
35        Self {
36            code,
37            message: message.into(),
38        }
39    }
40}
41
42impl From<reqwest::Error> for PayError {
43    fn from(e: reqwest::Error) -> Self {
44        PayError::new(PayErrorCode::HttpTransport, e.to_string())
45    }
46}
47
48impl From<serde_json::Error> for PayError {
49    fn from(e: serde_json::Error) -> Self {
50        PayError::new(PayErrorCode::ProtocolMalformed, format!("json: {e}"))
51    }
52}