relay filter/appview bootstrap
at main 1.6 kB view raw
1use axum::{ 2 http::StatusCode, 3 response::{IntoResponse, Response}, 4 Json, 5}; 6use serde::Serialize; 7 8#[derive(Debug, Serialize)] 9pub struct XrpcError { 10 pub error: &'static str, 11 pub message: String, 12} 13 14impl XrpcError { 15 pub fn method_not_found(path: &str) -> Self { 16 Self { 17 error: "MethodNotFound", 18 message: format!("Method not found: {}", path), 19 } 20 } 21 22 pub fn invalid_request(message: impl Into<String>) -> Self { 23 Self { 24 error: "InvalidRequest", 25 message: message.into(), 26 } 27 } 28 29 pub fn internal_error(message: impl Into<String>) -> Self { 30 Self { 31 error: "InternalServerError", 32 message: message.into(), 33 } 34 } 35} 36 37pub enum ApiError { 38 MethodNotFound(String), 39 InvalidRequest(String), 40 Internal(String), 41} 42 43impl IntoResponse for ApiError { 44 fn into_response(self) -> Response { 45 let (status, error) = match self { 46 ApiError::MethodNotFound(path) => { 47 (StatusCode::NOT_FOUND, XrpcError::method_not_found(&path)) 48 } 49 ApiError::InvalidRequest(msg) => { 50 (StatusCode::BAD_REQUEST, XrpcError::invalid_request(msg)) 51 } 52 ApiError::Internal(msg) => { 53 (StatusCode::INTERNAL_SERVER_ERROR, XrpcError::internal_error(msg)) 54 } 55 }; 56 (status, Json(error)).into_response() 57 } 58} 59 60impl From<sqlx::Error> for ApiError { 61 fn from(e: sqlx::Error) -> Self { 62 ApiError::Internal(e.to_string()) 63 } 64}