This maybe a step in the right direction.
This commit is contained in:
+117
-113
@@ -1,3 +1,5 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
@@ -5,8 +7,8 @@ use axum::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
domain::DomainError,
|
domain::DomainError,
|
||||||
repository::{ReferenceRelation, ReferenceViolationKind, RepositoryError},
|
repository::RepositoryError,
|
||||||
routes::extractors::auth::AuthError,
|
routes::extractors::auth::{AuthError, AuthHeaderError, BearerError},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
@@ -38,115 +40,121 @@ pub enum AppError {
|
|||||||
|
|
||||||
impl IntoResponse for AppError {
|
impl IntoResponse for AppError {
|
||||||
fn into_response(self) -> Response {
|
fn into_response(self) -> Response {
|
||||||
match self {
|
let (status, message) = self.client_response();
|
||||||
Self::BadRequest(error) => {
|
|
||||||
tracing::debug!(%error, "bad request");
|
if status.is_server_error() {
|
||||||
return error_response(StatusCode::BAD_REQUEST, error);
|
tracing::error!(error = ?self, %status, "request failed");
|
||||||
}
|
} else if matches!(self, Self::Conflict(_)) {
|
||||||
Self::Conflict(error) => {
|
tracing::warn!(error = ?self, %status, "request rejected");
|
||||||
tracing::warn!(%error, "conflict");
|
} else {
|
||||||
return error_response(StatusCode::CONFLICT, error);
|
tracing::debug!(error = ?self, %status, "request rejected");
|
||||||
}
|
|
||||||
Self::NotFound => {
|
|
||||||
tracing::debug!("not found");
|
|
||||||
return error_response(StatusCode::NOT_FOUND, "Not found");
|
|
||||||
}
|
|
||||||
Self::Unauthorized(message) => {
|
|
||||||
tracing::debug!(?message, "unauthorized");
|
|
||||||
return error_response(StatusCode::UNAUTHORIZED, message);
|
|
||||||
}
|
|
||||||
Self::Authorization(AuthError::UnsupportedScheme) => {
|
|
||||||
tracing::debug!("authorization error: unsupported scheme");
|
|
||||||
return error_response(StatusCode::UNAUTHORIZED, "Unsupported scheme");
|
|
||||||
}
|
|
||||||
Self::Authorization(AuthError::Header(error)) => {
|
|
||||||
tracing::debug!(%error, "authorization header error");
|
|
||||||
return error_response(StatusCode::UNAUTHORIZED, error.client_error_message());
|
|
||||||
}
|
|
||||||
Self::Authorization(AuthError::Bearer(error)) => {
|
|
||||||
tracing::debug!(%error, "bearer token error");
|
|
||||||
return error_response(StatusCode::UNAUTHORIZED, error.client_error_message());
|
|
||||||
}
|
|
||||||
Self::Authorization(AuthError::Repository(error)) => {
|
|
||||||
tracing::debug!(%error, "repository error");
|
|
||||||
}
|
|
||||||
Self::Domain(error) => {
|
|
||||||
tracing::debug!(%error, "domain error");
|
|
||||||
return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string());
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::Database(error)) => {
|
|
||||||
tracing::error!(%error, "database query failed");
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::Decode(error)) => {
|
|
||||||
tracing::error!(%error, "invalid stored data");
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::ReferenceViolation {
|
|
||||||
relation: ReferenceRelation::User,
|
|
||||||
kind: ReferenceViolationKind::Missing,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
tracing::debug!("user not found (reference violation)");
|
|
||||||
return error_response(StatusCode::NOT_FOUND, "User cannot be found");
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::ReferenceViolation {
|
|
||||||
relation: ReferenceRelation::UserApp,
|
|
||||||
kind: ReferenceViolationKind::Missing,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
tracing::debug!("user app not found (reference violation)");
|
|
||||||
return error_response(StatusCode::NOT_FOUND, "User app cannot be found");
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::ReferenceViolation {
|
|
||||||
relation: ReferenceRelation::Account,
|
|
||||||
kind: ReferenceViolationKind::Missing,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
tracing::debug!("account not found (reference violation)");
|
|
||||||
return error_response(StatusCode::UNPROCESSABLE_ENTITY, "Account cannot be found");
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::ReferenceViolation {
|
|
||||||
relation: ReferenceRelation::Transaction,
|
|
||||||
kind: ReferenceViolationKind::StillReferenced,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
tracing::debug!(
|
|
||||||
"still referenced by one or more transactions (reference violation)"
|
|
||||||
);
|
|
||||||
return error_response(
|
|
||||||
StatusCode::CONFLICT,
|
|
||||||
"Still referenced by one or more transactions",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::ReferenceViolation {
|
|
||||||
relation: ReferenceRelation::RecurringTransaction,
|
|
||||||
kind: ReferenceViolationKind::StillReferenced,
|
|
||||||
..
|
|
||||||
}) => {
|
|
||||||
tracing::debug!(
|
|
||||||
"still referenced by one or more recurring transactions (reference violation)"
|
|
||||||
);
|
|
||||||
return error_response(
|
|
||||||
StatusCode::CONFLICT,
|
|
||||||
"Still referenced by one or more recurring transactions",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::ReferenceViolation {
|
|
||||||
relation,
|
|
||||||
kind,
|
|
||||||
source,
|
|
||||||
}) => {
|
|
||||||
tracing::error!(%relation, %kind, error = ?source, "unhandled reference violation");
|
|
||||||
}
|
|
||||||
Self::Repository(RepositoryError::QueryConversion(error)) => {
|
|
||||||
tracing::debug!(%error, "query conversion error");
|
|
||||||
return error_response(StatusCode::BAD_REQUEST, error.to_string());
|
|
||||||
}
|
|
||||||
Self::Internal => {
|
|
||||||
tracing::error!("internal server error");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
error_response(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error")
|
error_response(status, message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait ClientError {
|
||||||
|
fn client_response(&self) -> (StatusCode, Cow<'_, str>);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn internal_error() -> (StatusCode, Cow<'static, str>) {
|
||||||
|
(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"Internal server error".into(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +172,3 @@ fn error_response(status_code: StatusCode, message: impl Into<String>) -> Respon
|
|||||||
)
|
)
|
||||||
.into_response()
|
.into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ClientError {
|
|
||||||
fn client_error_message(&self) -> &'static str;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -52,12 +52,3 @@ pub enum BearerError {
|
|||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Repository(#[from] RepositoryError),
|
Repository(#[from] RepositoryError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::error::ClientError for BearerError {
|
|
||||||
fn client_error_message(&self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
BearerError::InvalidCredentials => "Invalid credentials",
|
|
||||||
BearerError::Repository(_) => "Repository error",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -75,18 +75,6 @@ pub enum AuthHeaderError {
|
|||||||
EmptyToken,
|
EmptyToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::error::ClientError for AuthHeaderError {
|
|
||||||
fn client_error_message(&self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
AuthHeaderError::Missing => "Missing authorization header",
|
|
||||||
AuthHeaderError::NotAscii(_) => "Invalid authorization header encoding",
|
|
||||||
AuthHeaderError::MalformedValue => "Malformed authorization header",
|
|
||||||
AuthHeaderError::EmptyScheme => "Empty authorization scheme",
|
|
||||||
AuthHeaderError::EmptyToken => "Empty authorization token",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoResponse for AuthHeaderError {
|
impl IntoResponse for AuthHeaderError {
|
||||||
fn into_response(self) -> axum::response::Response {
|
fn into_response(self) -> axum::response::Response {
|
||||||
Into::<super::AuthError>::into(self).into_response()
|
Into::<super::AuthError>::into(self).into_response()
|
||||||
|
|||||||
Reference in New Issue
Block a user