|
| 1 | +// use std::fmt::Display; |
| 2 | + |
| 3 | +use axum::{extract::multipart::MultipartError, http::StatusCode, response::IntoResponse, Json}; |
| 4 | +use blake3::HexError; |
| 5 | +use redb::{CommitError, DatabaseError, StorageError, TableError, TransactionError}; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use thiserror::Error; |
| 8 | +use tracing::subscriber::SetGlobalDefaultError; |
| 9 | +use utoipa::ToSchema; |
| 10 | + |
| 11 | +#[derive(Debug, Error)] |
| 12 | +pub enum UsubaError { |
| 13 | + #[error("Bad request body")] |
| 14 | + BadRequest, |
| 15 | + #[error("Failed to bake the module: {0}")] |
| 16 | + BakeFailure(String), |
| 17 | + #[error("Invalid configuration: {0}")] |
| 18 | + InvalidConfiguration(String), |
| 19 | + #[error("Module not found")] |
| 20 | + ModuleNotFound, |
| 21 | + #[error("An internal error occurred")] |
| 22 | + Internal(String), |
| 23 | +} |
| 24 | + |
| 25 | +impl From<std::net::AddrParseError> for UsubaError { |
| 26 | + fn from(value: std::net::AddrParseError) -> Self { |
| 27 | + UsubaError::InvalidConfiguration(format!("{}", value)) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl From<std::io::Error> for UsubaError { |
| 32 | + fn from(value: std::io::Error) -> Self { |
| 33 | + error!("{}", value); |
| 34 | + UsubaError::Internal(format!("{}", value)) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl From<MultipartError> for UsubaError { |
| 39 | + fn from(_value: MultipartError) -> Self { |
| 40 | + UsubaError::BadRequest |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl From<SetGlobalDefaultError> for UsubaError { |
| 45 | + fn from(value: SetGlobalDefaultError) -> Self { |
| 46 | + error!("{}", value); |
| 47 | + UsubaError::Internal(format!("{}", value)) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl From<StorageError> for UsubaError { |
| 52 | + fn from(value: StorageError) -> Self { |
| 53 | + error!("{}", value); |
| 54 | + UsubaError::ModuleNotFound |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl From<TransactionError> for UsubaError { |
| 59 | + fn from(value: TransactionError) -> Self { |
| 60 | + error!("{}", value); |
| 61 | + UsubaError::Internal(format!("{}", value)) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl From<TableError> for UsubaError { |
| 66 | + fn from(value: TableError) -> Self { |
| 67 | + error!("{}", value); |
| 68 | + UsubaError::Internal(format!("{}", value)) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl From<CommitError> for UsubaError { |
| 73 | + fn from(value: CommitError) -> Self { |
| 74 | + error!("{}", value); |
| 75 | + UsubaError::Internal(format!("{}", value)) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl From<DatabaseError> for UsubaError { |
| 80 | + fn from(value: DatabaseError) -> Self { |
| 81 | + error!("{}", value); |
| 82 | + UsubaError::Internal(format!("{}", value)) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl From<HexError> for UsubaError { |
| 87 | + fn from(_value: HexError) -> Self { |
| 88 | + UsubaError::BadRequest |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl IntoResponse for UsubaError { |
| 93 | + fn into_response(self) -> axum::response::Response { |
| 94 | + let status = match self { |
| 95 | + UsubaError::BadRequest => StatusCode::BAD_REQUEST, |
| 96 | + UsubaError::BakeFailure(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 97 | + UsubaError::InvalidConfiguration(_) => StatusCode::BAD_REQUEST, |
| 98 | + UsubaError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 99 | + UsubaError::ModuleNotFound => StatusCode::NOT_FOUND, |
| 100 | + }; |
| 101 | + |
| 102 | + ( |
| 103 | + status, |
| 104 | + Json(ErrorResponse { |
| 105 | + error: self.to_string(), |
| 106 | + }), |
| 107 | + ) |
| 108 | + .into_response() |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[derive(Serialize, Deserialize, ToSchema)] |
| 113 | +pub struct ErrorResponse { |
| 114 | + error: String, |
| 115 | +} |
0 commit comments