baobab/proxies/http/src/error.rs
2025-07-29 01:15:23 +02:00

64 lines
1.9 KiB
Rust

use actix_web::{HttpResponse, ResponseError};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ProxyError {
#[error("WebSocket connection error: {0}")]
WebSocketError(String),
#[error("JSON-RPC error: {0}")]
JsonRpcError(String),
#[error("Webhook signature verification failed: {0}")]
SignatureVerificationError(String),
#[error("Invalid webhook payload: {0}")]
InvalidPayload(String),
#[error("Configuration error: {0}")]
ConfigError(String),
#[error("Timeout error: {0}")]
TimeoutError(String),
#[error("Serialization error: {0}")]
SerializationError(#[from] serde_json::Error),
#[error("Network error: {0}")]
NetworkError(#[from] reqwest::Error),
#[error("WebSocket error: {0}")]
TungsteniteError(#[from] tokio_tungstenite::tungstenite::Error),
}
impl ResponseError for ProxyError {
fn error_response(&self) -> HttpResponse {
match self {
ProxyError::SignatureVerificationError(_) => {
HttpResponse::Unauthorized().json(serde_json::json!({
"error": "signature_verification_failed",
"message": self.to_string()
}))
}
ProxyError::InvalidPayload(_) => {
HttpResponse::BadRequest().json(serde_json::json!({
"error": "invalid_payload",
"message": self.to_string()
}))
}
ProxyError::TimeoutError(_) => {
HttpResponse::RequestTimeout().json(serde_json::json!({
"error": "timeout",
"message": self.to_string()
}))
}
_ => {
HttpResponse::InternalServerError().json(serde_json::json!({
"error": "internal_server_error",
"message": self.to_string()
}))
}
}
}
}