1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
6#[serde(rename_all = "snake_case")]
7pub enum BridgeErrorCode {
8 InvalidRequest,
10 AuthenticationFailed,
12 InsufficientCredit,
14 RequestConflict,
16 RateLimited,
18 NetworkError,
20 ServerError,
22 UnknownError,
24}
25
26impl BridgeErrorCode {
27 pub fn as_str(&self) -> &'static str {
29 match self {
30 Self::InvalidRequest => "invalid_request",
31 Self::AuthenticationFailed => "authentication_failed",
32 Self::InsufficientCredit => "insufficient_credit",
33 Self::RequestConflict => "request_conflict",
34 Self::RateLimited => "rate_limited",
35 Self::NetworkError => "network_error",
36 Self::ServerError => "server_error",
37 Self::UnknownError => "unknown_error",
38 }
39 }
40}
41
42#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
44pub struct BridgeError {
45 pub code: String,
47 pub message: String,
49 pub retry_after_ms: Option<u64>,
51 pub details: Option<Value>,
53}
54
55impl BridgeError {
56 pub fn new(code: BridgeErrorCode, message: impl Into<String>) -> Self {
58 Self {
59 code: code.as_str().to_owned(),
60 message: message.into(),
61 retry_after_ms: None,
62 details: None,
63 }
64 }
65
66 pub fn with_retry_after_ms(mut self, retry_after_ms: Option<u64>) -> Self {
68 self.retry_after_ms = retry_after_ms;
69 self
70 }
71
72 pub fn with_details(mut self, details: Value) -> Self {
74 self.details = Some(details);
75 self
76 }
77
78 pub fn invalid_request(message: impl Into<String>) -> Self {
80 Self::new(BridgeErrorCode::InvalidRequest, message)
81 }
82
83 pub fn authentication_failed(message: impl Into<String>) -> Self {
85 Self::new(BridgeErrorCode::AuthenticationFailed, message)
86 }
87
88 pub fn insufficient_credit(message: impl Into<String>) -> Self {
90 Self::new(BridgeErrorCode::InsufficientCredit, message)
91 }
92
93 pub fn request_conflict(message: impl Into<String>) -> Self {
95 Self::new(BridgeErrorCode::RequestConflict, message)
96 }
97
98 pub fn rate_limited(message: impl Into<String>, retry_after_ms: Option<u64>) -> Self {
100 Self::new(BridgeErrorCode::RateLimited, message).with_retry_after_ms(retry_after_ms)
101 }
102
103 pub fn network_error(message: impl Into<String>) -> Self {
105 Self::new(BridgeErrorCode::NetworkError, message)
106 }
107
108 pub fn server_error(message: impl Into<String>) -> Self {
110 Self::new(BridgeErrorCode::ServerError, message)
111 }
112
113 pub fn unknown_error(message: impl Into<String>) -> Self {
115 Self::new(BridgeErrorCode::UnknownError, message)
116 }
117}
118
119impl core::fmt::Display for BridgeError {
120 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
121 write!(f, "{}: {}", self.code, self.message)
122 }
123}
124
125impl std::error::Error for BridgeError {}