|
| 1 | +//! Unified error types for the StellarAid blockchain integration layer. |
| 2 | +//! |
| 3 | +//! [`StellarAidError`] is the top-level error enum that downstream code should |
| 4 | +//! use as its return type. Each variant maps to a distinct failure domain and |
| 5 | +//! carries enough context for callers to decide how to recover (or surface a |
| 6 | +//! useful message). |
| 7 | +//! |
| 8 | +//! Existing per-module error types ([`KeyError`](crate::utils::keypair::KeyError), |
| 9 | +//! [`StellarError`](crate::friendbot::utils::types::StellarError), |
| 10 | +//! [`TokenSetupError`](crate::setup::token_setup::TokenSetupError)) are |
| 11 | +//! automatically converted via [`From`] impls so the `?` operator works |
| 12 | +//! transparently. |
| 13 | +
|
| 14 | +use thiserror::Error; |
| 15 | + |
| 16 | +use crate::friendbot::utils::types::StellarError; |
| 17 | +use crate::setup::token_setup::TokenSetupError; |
| 18 | +use crate::utils::keypair::KeyError; |
| 19 | + |
| 20 | +/// Top-level error type for the StellarAid integration layer. |
| 21 | +#[derive(Debug, Error)] |
| 22 | +pub enum StellarAidError { |
| 23 | + /// Stellar Horizon REST-API returned an error or an unexpected response. |
| 24 | + #[error("Horizon API error: {0}")] |
| 25 | + HorizonError(String), |
| 26 | + |
| 27 | + /// Soroban JSON-RPC call failed. |
| 28 | + #[error("Soroban RPC error (code {code}): {message}")] |
| 29 | + SorobanError { code: i64, message: String }, |
| 30 | + |
| 31 | + /// Key generation, parsing, or derivation failed. |
| 32 | + #[error("Keypair error: {0}")] |
| 33 | + KeypairError(String), |
| 34 | + |
| 35 | + /// Input or state did not meet a business-logic precondition. |
| 36 | + #[error("Validation error: {0}")] |
| 37 | + ValidationError(String), |
| 38 | + |
| 39 | + /// A submitted transaction was rejected or reverted by the network. |
| 40 | + #[error("Transaction failed: {0}")] |
| 41 | + TransactionFailed(String), |
| 42 | + |
| 43 | + /// An on-chain smart-contract call returned an error. |
| 44 | + #[error("Contract error: {0}")] |
| 45 | + ContractError(String), |
| 46 | + |
| 47 | + /// A lower-level network / HTTP / I/O error. |
| 48 | + #[error("Network error: {0}")] |
| 49 | + NetworkError(String), |
| 50 | +} |
| 51 | + |
| 52 | +// ── From impls for ergonomic `?` propagation ──────────────────────────────── |
| 53 | + |
| 54 | +impl From<KeyError> for StellarAidError { |
| 55 | + fn from(err: KeyError) -> Self { |
| 56 | + Self::KeypairError(err.to_string()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl From<StellarError> for StellarAidError { |
| 61 | + fn from(err: StellarError) -> Self { |
| 62 | + match &err { |
| 63 | + StellarError::FriendbotNotAvailable { .. } => Self::NetworkError(err.to_string()), |
| 64 | + StellarError::HttpRequestFailed(_) => Self::NetworkError(err.to_string()), |
| 65 | + StellarError::FriendbotError { .. } => Self::HorizonError(err.to_string()), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl From<TokenSetupError> for StellarAidError { |
| 71 | + fn from(err: TokenSetupError) -> Self { |
| 72 | + Self::ContractError(err.to_string()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl From<reqwest::Error> for StellarAidError { |
| 77 | + fn from(err: reqwest::Error) -> Self { |
| 78 | + Self::NetworkError(err.to_string()) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + use crate::friendbot::utils::types::StellarError; |
| 86 | + use crate::setup::token_setup::TokenSetupError; |
| 87 | + use crate::utils::keypair::KeyError; |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn key_error_converts_to_keypair_variant() { |
| 91 | + let err: StellarAidError = KeyError::InvalidSecretKey("bad".into()).into(); |
| 92 | + assert!(matches!(err, StellarAidError::KeypairError(_))); |
| 93 | + assert!(err.to_string().contains("bad")); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn stellar_friendbot_not_available_converts_to_network() { |
| 98 | + let err: StellarAidError = StellarError::FriendbotNotAvailable { |
| 99 | + network: "mainnet".into(), |
| 100 | + } |
| 101 | + .into(); |
| 102 | + assert!(matches!(err, StellarAidError::NetworkError(_))); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn stellar_http_failure_converts_to_network() { |
| 107 | + let err: StellarAidError = StellarError::HttpRequestFailed("timeout".into()).into(); |
| 108 | + assert!(matches!(err, StellarAidError::NetworkError(_))); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn stellar_friendbot_error_converts_to_horizon() { |
| 113 | + let err: StellarAidError = StellarError::FriendbotError { |
| 114 | + status: 500, |
| 115 | + body: "internal".into(), |
| 116 | + } |
| 117 | + .into(); |
| 118 | + assert!(matches!(err, StellarAidError::HorizonError(_))); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn token_setup_error_converts_to_contract() { |
| 123 | + let err: StellarAidError = TokenSetupError::CommandFailed { |
| 124 | + command: "stellar deploy".into(), |
| 125 | + stderr: "oops".into(), |
| 126 | + } |
| 127 | + .into(); |
| 128 | + assert!(matches!(err, StellarAidError::ContractError(_))); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn display_includes_variant_prefix() { |
| 133 | + let err = StellarAidError::ValidationError("amount must be positive".into()); |
| 134 | + assert_eq!(err.to_string(), "Validation error: amount must be positive"); |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn soroban_error_formats_code_and_message() { |
| 139 | + let err = StellarAidError::SorobanError { |
| 140 | + code: -32600, |
| 141 | + message: "invalid request".into(), |
| 142 | + }; |
| 143 | + assert!(err.to_string().contains("-32600")); |
| 144 | + assert!(err.to_string().contains("invalid request")); |
| 145 | + } |
| 146 | +} |
0 commit comments