175 lines
5.6 KiB
Rust
175 lines
5.6 KiB
Rust
use std::borrow::Cow;
|
|
|
|
use axum::{
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
|
|
use crate::{
|
|
domain::DomainError,
|
|
repository::RepositoryError,
|
|
routes::extractors::auth::{AuthError, AuthHeaderError, BearerError},
|
|
};
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum AppError {
|
|
#[error("bad request")]
|
|
BadRequest(String),
|
|
|
|
#[error("conflict")]
|
|
Conflict(String),
|
|
|
|
#[error("not found")]
|
|
NotFound,
|
|
|
|
#[error("unauthorized")]
|
|
Unauthorized(String),
|
|
|
|
#[error("authorization error")]
|
|
Authorization(#[from] AuthError),
|
|
|
|
#[error("domain error")]
|
|
Domain(#[from] DomainError),
|
|
|
|
#[error("repository error")]
|
|
Repository(#[from] RepositoryError),
|
|
|
|
#[error("internal server error")]
|
|
Internal,
|
|
}
|
|
|
|
impl IntoResponse for AppError {
|
|
fn into_response(self) -> Response {
|
|
let (status, message) = self.client_response();
|
|
|
|
if status.is_server_error() {
|
|
tracing::error!(error = ?self, %status, "request failed");
|
|
} else if matches!(self, Self::Conflict(_)) {
|
|
tracing::warn!(error = ?self, %status, "request rejected");
|
|
} else {
|
|
tracing::debug!(error = ?self, %status, "request rejected");
|
|
}
|
|
|
|
error_response(status, message)
|
|
}
|
|
}
|
|
|
|
impl ClientError for AppError {
|
|
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
|
match self {
|
|
Self::BadRequest(message) => (StatusCode::BAD_REQUEST, message.as_str().into()),
|
|
Self::Unauthorized(message) => (StatusCode::UNAUTHORIZED, message.as_str().into()),
|
|
Self::NotFound => (StatusCode::NOT_FOUND, "Not found".into()),
|
|
Self::Conflict(message) => (StatusCode::CONFLICT, message.as_str().into()),
|
|
Self::Authorization(error) => error.client_response(),
|
|
Self::Domain(error) => error.client_response(),
|
|
Self::Repository(error) => error.client_response(),
|
|
Self::Internal => internal_error(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ClientError for AuthError {
|
|
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
|
match self {
|
|
Self::UnsupportedScheme => (StatusCode::UNAUTHORIZED, "Unsupported scheme".into()),
|
|
Self::Bearer(error) => error.client_response(),
|
|
Self::Header(error) => error.client_response(),
|
|
Self::Repository(error) => error.client_response(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ClientError for AuthHeaderError {
|
|
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
|
let message = match self {
|
|
Self::Missing => "Missing authorization header",
|
|
Self::NotAscii(_) => "Invalid authorization header encoding",
|
|
Self::MalformedValue => "Malformed authorization header",
|
|
Self::EmptyScheme => "Empty authorization scheme",
|
|
Self::EmptyToken => "Empty authorization token",
|
|
};
|
|
|
|
(StatusCode::UNAUTHORIZED, message.into())
|
|
}
|
|
}
|
|
|
|
impl ClientError for BearerError {
|
|
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
|
match self {
|
|
Self::InvalidCredentials => (StatusCode::UNAUTHORIZED, "Invalid credentials".into()),
|
|
Self::Repository(error) => error.client_response(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ClientError for DomainError {
|
|
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
|
match self {
|
|
Self::Amount(error) => (StatusCode::UNPROCESSABLE_ENTITY, error.to_string().into()),
|
|
Self::Schedule(error) => (StatusCode::UNPROCESSABLE_ENTITY, error.to_string().into()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ClientError for RepositoryError {
|
|
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
|
match self {
|
|
Self::Database(_) | Self::Decode(_) => internal_error(),
|
|
Self::QueryConversion(error) => (StatusCode::BAD_REQUEST, error.to_string().into()),
|
|
Self::ReferenceViolation { relation, kind, .. } => {
|
|
use crate::repository::{
|
|
ReferenceRelation as Relation, ReferenceViolationKind as Kind,
|
|
};
|
|
match (relation, kind) {
|
|
(Relation::Account, Kind::Missing) => (
|
|
StatusCode::UNPROCESSABLE_ENTITY,
|
|
"Account cannot be found".into(),
|
|
),
|
|
(Relation::RecurringTransaction, Kind::StillReferenced) => (
|
|
StatusCode::CONFLICT,
|
|
"Still referenced by one or more recurring transactions".into(),
|
|
),
|
|
(Relation::Transaction, Kind::StillReferenced) => (
|
|
StatusCode::CONFLICT,
|
|
"Still referenced by one or more transactions".into(),
|
|
),
|
|
(Relation::User, Kind::Missing) => {
|
|
(StatusCode::NOT_FOUND, "User cannot be found".into())
|
|
}
|
|
(Relation::UserApp, Kind::Missing) => {
|
|
(StatusCode::NOT_FOUND, "User app cannot be found".into())
|
|
}
|
|
_ => internal_error(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
trait ClientError {
|
|
fn client_response(&self) -> (StatusCode, Cow<'_, str>);
|
|
}
|
|
|
|
fn internal_error() -> (StatusCode, Cow<'static, str>) {
|
|
(
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
"Internal server error".into(),
|
|
)
|
|
}
|
|
|
|
#[derive(serde::Serialize)]
|
|
struct ErrorResponse {
|
|
pub message: String,
|
|
}
|
|
|
|
fn error_response(status_code: StatusCode, message: impl Into<String>) -> Response {
|
|
(
|
|
status_code,
|
|
axum::Json(ErrorResponse {
|
|
message: message.into(),
|
|
}),
|
|
)
|
|
.into_response()
|
|
}
|