These errors have become app level errors.

This commit is contained in:
2026-10-09 15:28:27 +02:00
parent 66984567f8
commit 18e142ec91
4 changed files with 11 additions and 40 deletions
+7 -24
View File
@@ -13,6 +13,9 @@ pub enum AppError {
#[error("bad request")] #[error("bad request")]
BadRequest(String), BadRequest(String),
#[error("conflict")]
Conflict(String),
#[error("not found")] #[error("not found")]
NotFound, NotFound,
@@ -36,6 +39,10 @@ impl IntoResponse for AppError {
tracing::debug!(%error, "bad request"); tracing::debug!(%error, "bad request");
return error_response(StatusCode::BAD_REQUEST, error); return error_response(StatusCode::BAD_REQUEST, error);
} }
Self::Conflict(error) => {
tracing::warn!(%error, "conflict");
return error_response(StatusCode::CONFLICT, error);
}
Self::NotFound => { Self::NotFound => {
tracing::debug!("not found"); tracing::debug!("not found");
return error_response(StatusCode::NOT_FOUND, "Not found"); return error_response(StatusCode::NOT_FOUND, "Not found");
@@ -54,30 +61,6 @@ impl IntoResponse for AppError {
Self::Repository(RepositoryError::Decode(error)) => { Self::Repository(RepositoryError::Decode(error)) => {
tracing::error!(%error, "invalid stored data"); tracing::error!(%error, "invalid stored data");
} }
Self::Repository(RepositoryError::NotFound {
relation: ReferenceRelation::User,
}) => {
tracing::debug!("user not found");
return error_response(StatusCode::NOT_FOUND, "User cannot be found");
}
Self::Repository(RepositoryError::NotFound {
relation: ReferenceRelation::UserApp,
}) => {
tracing::debug!("user app not found");
return error_response(StatusCode::NOT_FOUND, "User app cannot be found");
}
Self::Repository(RepositoryError::NotFound { relation }) => {
tracing::error!(%relation, "unhandled not found");
}
Self::Repository(RepositoryError::OwnershipConflict {
relation: ReferenceRelation::UserApp,
}) => {
tracing::warn!("user app ownership conflict");
return error_response(StatusCode::CONFLICT, "User app ID is already in use");
}
Self::Repository(RepositoryError::OwnershipConflict { relation }) => {
tracing::error!(%relation, "unhandled ownership conflict");
}
Self::Repository(RepositoryError::ReferenceViolation { Self::Repository(RepositoryError::ReferenceViolation {
relation: ReferenceRelation::User, relation: ReferenceRelation::User,
kind: ReferenceViolationKind::Missing, kind: ReferenceViolationKind::Missing,
-6
View File
@@ -21,12 +21,6 @@ pub enum RepositoryError {
#[error(transparent)] #[error(transparent)]
Decode(#[from] DecodeError), Decode(#[from] DecodeError),
#[error("{relation} cannot be found")]
NotFound { relation: ReferenceRelation },
#[error("{relation} is owned by another user")]
OwnershipConflict { relation: ReferenceRelation },
#[error("reference violation ({kind}: {relation})")] #[error("reference violation ({kind}: {relation})")]
ReferenceViolation { ReferenceViolation {
relation: ReferenceRelation, relation: ReferenceRelation,
+2 -5
View File
@@ -13,7 +13,7 @@ use crate::{
crypto::{generate_secret, hash_secret}, crypto::{generate_secret, hash_secret},
domain::UserAppSecret, domain::UserAppSecret,
error::AppError, error::AppError,
repository::{self, ReferenceRelation, RepositoryError}, repository::{self, RepositoryError},
}; };
pub fn create_router() -> axum::Router<PgPool> { pub fn create_router() -> axum::Router<PgPool> {
@@ -141,9 +141,6 @@ async fn ensure_app_ownership(
) -> Result<(), AppError> { ) -> Result<(), AppError> {
match repository::user_apps::find_owner_by_id(&mut *connection, app_id).await? { match repository::user_apps::find_owner_by_id(&mut *connection, app_id).await? {
Some(owner) if owner == user_id => Ok(()), Some(owner) if owner == user_id => Ok(()),
Some(_) | None => Err(RepositoryError::NotFound { Some(_) | None => Err(AppError::NotFound).into(),
relation: ReferenceRelation::UserApp,
}
.into()),
} }
} }
+2 -5
View File
@@ -9,7 +9,7 @@ use super::{
}; };
use crate::{ use crate::{
error::AppError, error::AppError,
repository::{self, ReferenceRelation, RepositoryError}, repository::{self, RepositoryError},
}; };
pub fn create_router() -> axum::Router<PgPool> { pub fn create_router() -> axum::Router<PgPool> {
@@ -81,10 +81,7 @@ async fn upsert_user_app(
.as_ref() .as_ref()
.is_some_and(|previous| previous.user_id != user_id) .is_some_and(|previous| previous.user_id != user_id)
{ {
return Err(RepositoryError::OwnershipConflict { return Err(AppError::Conflict("User app ID is already in use".into()).into());
relation: ReferenceRelation::UserApp,
}
.into());
} }
tx.commit().await.map_err(RepositoryError::from)?; tx.commit().await.map_err(RepositoryError::from)?;