Spaces:
Sleeping
Sleeping
Download src/errors.rs from nishantcoder/aura-rs-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.6 kB
-
https://huggingface.co/spaces/nishantcoder/aura-rs-backend/resolve/main/src/errors.rs
- Command line
-
hf download hf://spaces/nishantcoder/aura-rs-backend/src/errors.rs
-
curl -L -o errors.rs https://huggingface.co/spaces/nishantcoder/aura-rs-backend/resolve/main/src/errors.rs
2.6 kB
| use axum::{ | |
| Json, | |
| http::StatusCode, | |
| response::{IntoResponse, Response}, | |
| }; | |
| use serde::Serialize; | |
| use thiserror::Error; | |
| pub type AppResult<T> = Result<T, AppError>; | |
| pub enum AppError { | |
| BadRequest(String), | |
| Unauthorized(String), | |
| NotFound(String), | |
| Conflict(String), | |
| Config(String), | |
| Database( sqlx::Error), | |
| Migration( sqlx::migrate::MigrateError), | |
| Jwt( jsonwebtoken::errors::Error), | |
| Io( std::io::Error), | |
| Internal, | |
| } | |
| struct ErrorBody { | |
| error: String, | |
| } | |
| impl AppError { | |
| fn status_code(&self) -> StatusCode { | |
| match self { | |
| Self::BadRequest(_) | Self::Config(_) => StatusCode::BAD_REQUEST, | |
| Self::Unauthorized(_) => StatusCode::UNAUTHORIZED, | |
| Self::NotFound(_) => StatusCode::NOT_FOUND, | |
| Self::Conflict(_) => StatusCode::CONFLICT, | |
| Self::Database(_) | |
| | Self::Migration(_) | |
| | Self::Jwt(_) | |
| | Self::Io(_) | |
| | Self::Internal => StatusCode::INTERNAL_SERVER_ERROR, | |
| } | |
| } | |
| fn message(&self) -> String { | |
| match self { | |
| Self::BadRequest(message) | |
| | Self::Unauthorized(message) | |
| | Self::NotFound(message) | |
| | Self::Conflict(message) | |
| | Self::Config(message) => message.clone(), | |
| Self::Database(error) => { | |
| tracing::error!("database error: {error}"); | |
| "Database request failed".to_owned() | |
| } | |
| Self::Migration(error) => { | |
| tracing::error!("migration error: {error}"); | |
| "Database migration failed".to_owned() | |
| } | |
| Self::Jwt(error) => { | |
| tracing::error!("jwt error: {error}"); | |
| "Authentication failed".to_owned() | |
| } | |
| Self::Io(error) => { | |
| tracing::error!("io error: {error}"); | |
| "Server IO failed".to_owned() | |
| } | |
| Self::Internal => "Internal server error".to_owned(), | |
| } | |
| } | |
| } | |
| impl IntoResponse for AppError { | |
| fn into_response(self) -> Response { | |
| let status = self.status_code(); | |
| let body = Json(ErrorBody { | |
| error: self.message(), | |
| }); | |
| (status, body).into_response() | |
| } | |
| } | |