Continuing with error refactoring.

This commit is contained in:
2026-10-10 20:50:43 +02:00
parent d6bb67721e
commit 731139045d
18 changed files with 359 additions and 103 deletions
+5 -9
View File
@@ -43,16 +43,12 @@ async fn issue_token(
.map_err(repository::RepositoryError::from)?;
if !repository::user_app_secrets::verify(&mut *tx, client_id, &secret_hash).await? {
return Err(AppError::Unauthorized(
"Invalid client credentials".to_string(),
));
return Err(AppError::InvalidClientCredentials);
}
let user_app = repository::user_apps::get_by_id(&mut *tx, client_id)
.await?
.ok_or(AppError::Unauthorized(
"Invalid client credentials".to_string(),
))?;
.ok_or(AppError::InvalidClientCredentials)?;
repository::tokens::issue(
&mut *tx,
@@ -86,7 +82,7 @@ async fn issue_token(
)
.await?;
if !rotated {
return Err(AppError::Unauthorized("Invalid refresh token".to_string()));
return Err(AppError::InvalidRefreshToken);
}
tx.commit()
.await
@@ -119,8 +115,8 @@ struct TokenPair {
}
fn generate_token_pair() -> Result<TokenPair, AppError> {
let access_token = generate_secret().map_err(|_| AppError::Internal)?;
let refresh_token = generate_secret().map_err(|_| AppError::Internal)?;
let access_token = generate_secret().map_err(|_| AppError::Unknown)?;
let refresh_token = generate_secret().map_err(|_| AppError::Unknown)?;
let now = Utc::now();
Ok(TokenPair {
access_token_hash: hash_secret(&access_token),
+22 -4
View File
@@ -9,6 +9,24 @@ pub struct UserAppSecretRequest {
pub expires_at: DateTime<Utc>,
}
impl UserAppSecretRequest {
pub fn into_domain(
self,
id: Uuid,
app_id: Uuid,
secret_hash: Vec<u8>,
created_at: DateTime<Utc>,
) -> Result<domain::UserAppSecret, domain::DomainError> {
Ok(domain::UserAppSecret::new(
id,
app_id,
secret_hash,
self.expires_at,
created_at,
)?)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct UserAppSecretResponse {
pub id: Uuid,
@@ -20,10 +38,10 @@ pub struct UserAppSecretResponse {
impl From<domain::UserAppSecret> for UserAppSecretResponse {
fn from(secret: domain::UserAppSecret) -> Self {
Self {
id: secret.id,
app_id: secret.app_id,
expires_at: secret.expires_at,
created_at: secret.created_at,
id: secret.id(),
app_id: secret.app_id(),
expires_at: secret.expires_at(),
created_at: secret.created_at(),
}
}
}
+28
View File
@@ -1,5 +1,33 @@
use axum::response::IntoResponse;
use crate::error::AppError;
pub mod auth;
pub mod form;
pub mod json;
pub mod path;
pub mod query;
#[derive(Debug, thiserror::Error)]
pub enum ExtractorError {
#[error(transparent)]
Authorization(#[from] auth::AuthError),
#[error(transparent)]
Form(#[from] form::FormError),
#[error(transparent)]
Json(#[from] json::JsonError),
#[error(transparent)]
Path(#[from] path::PathError),
#[error(transparent)]
Query(#[from] query::QueryError),
}
impl IntoResponse for ExtractorError {
fn into_response(self) -> axum::response::Response {
AppError::from(self).into_response()
}
}
+2 -2
View File
@@ -8,7 +8,7 @@ pub use bearer::*;
pub use header::*;
pub use principal::*;
use crate::{error::AppError, repository::RepositoryError};
use crate::{repository::RepositoryError, routes::extractors::ExtractorError};
#[derive(Debug)]
pub struct Auth<T = Bearer>(pub Principal<T>);
@@ -55,7 +55,7 @@ pub enum AuthError {
impl IntoResponse for AuthError {
fn into_response(self) -> axum::response::Response {
AppError::from(self).into_response()
ExtractorError::from(self).into_response()
}
}
+17 -4
View File
@@ -1,7 +1,10 @@
use axum::extract::{FromRequest, Request};
use axum::{
extract::{FromRequest, Request},
response::IntoResponse,
};
use serde::de::DeserializeOwned;
use crate::error::AppError;
use crate::routes::extractors::ExtractorError;
pub struct Form<T>(pub T);
@@ -10,12 +13,22 @@ where
T: DeserializeOwned,
S: Send + Sync,
{
type Rejection = AppError;
type Rejection = FormError;
async fn from_request(request: Request, state: &S) -> Result<Self, Self::Rejection> {
axum::Form::<T>::from_request(request, state)
.await
.map(|axum::Form(value)| Self(value))
.map_err(|rejection| AppError::BadRequest(rejection.body_text()))
.map_err(Into::into)
}
}
#[derive(Debug, thiserror::Error)]
#[error("form error")]
pub struct FormError(#[from] axum::extract::rejection::FormRejection);
impl IntoResponse for FormError {
fn into_response(self) -> axum::response::Response {
ExtractorError::Form(self).into_response()
}
}
+13 -3
View File
@@ -3,7 +3,7 @@ use axum::response::IntoResponse;
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::error::AppError;
use crate::routes::extractors::ExtractorError;
pub struct Json<T>(pub T);
@@ -12,13 +12,13 @@ where
T: DeserializeOwned,
S: Send + Sync,
{
type Rejection = AppError;
type Rejection = JsonError;
async fn from_request(request: Request, state: &S) -> Result<Self, Self::Rejection> {
axum::Json::<T>::from_request(request, state)
.await
.map(|axum::Json(value)| Self(value))
.map_err(|rejection| AppError::BadRequest(rejection.body_text()))
.map_err(Into::into)
}
}
@@ -30,3 +30,13 @@ where
axum::Json(self.0).into_response()
}
}
#[derive(Debug, thiserror::Error)]
#[error("json error")]
pub struct JsonError(#[from] axum::extract::rejection::JsonRejection);
impl IntoResponse for JsonError {
fn into_response(self) -> axum::response::Response {
ExtractorError::Json(self).into_response()
}
}
+14 -3
View File
@@ -1,7 +1,8 @@
use axum::extract::FromRequestParts;
use axum::response::IntoResponse;
use serde::de::DeserializeOwned;
use crate::error::AppError;
use crate::routes::extractors::ExtractorError;
pub struct Path<T>(pub T);
@@ -10,7 +11,7 @@ where
T: DeserializeOwned + Send,
S: Send + Sync,
{
type Rejection = AppError;
type Rejection = PathError;
async fn from_request_parts(
parts: &mut axum::http::request::Parts,
@@ -19,6 +20,16 @@ where
axum::extract::Path::<T>::from_request_parts(parts, state)
.await
.map(|axum::extract::Path(value)| Self(value))
.map_err(|rejection| AppError::BadRequest(rejection.body_text()))
.map_err(Into::into)
}
}
#[derive(Debug, thiserror::Error)]
#[error("path error")]
pub struct PathError(#[from] axum::extract::rejection::PathRejection);
impl IntoResponse for PathError {
fn into_response(self) -> axum::response::Response {
ExtractorError::Path(self).into_response()
}
}
+14 -3
View File
@@ -1,7 +1,8 @@
use axum::extract::FromRequestParts;
use axum::response::IntoResponse;
use serde::de::DeserializeOwned;
use crate::error::AppError;
use crate::routes::extractors::ExtractorError;
pub struct Query<T>(pub T);
@@ -10,7 +11,7 @@ where
T: DeserializeOwned + Send,
S: Send + Sync,
{
type Rejection = AppError;
type Rejection = QueryError;
async fn from_request_parts(
parts: &mut axum::http::request::Parts,
@@ -19,6 +20,16 @@ where
axum::extract::Query::<T>::from_request_parts(parts, state)
.await
.map(|axum::extract::Query(value)| Self(value))
.map_err(|rejection| AppError::BadRequest(rejection.body_text()))
.map_err(Into::into)
}
}
#[derive(Debug, thiserror::Error)]
#[error("query error")]
pub struct QueryError(#[from] axum::extract::rejection::QueryRejection);
impl IntoResponse for QueryError {
fn into_response(self) -> axum::response::Response {
ExtractorError::Query(self).into_response()
}
}
+1 -1
View File
@@ -144,7 +144,7 @@ fn create_response(
let account = accounts
.get(&transaction.account_id)
.cloned()
.ok_or(AppError::Internal)?;
.ok_or(AppError::Unknown)?;
let next = transaction.schedule.next_occurrence(now);
+1 -1
View File
@@ -135,7 +135,7 @@ fn create_response(
let account = accounts
.get(&transaction.account_id)
.cloned()
.ok_or(AppError::Internal)?;
.ok_or(AppError::Unknown)?;
Ok(TransactionResponse::new(transaction, account))
}
+3 -15
View File
@@ -11,7 +11,6 @@ use super::{
};
use crate::{
crypto::{generate_secret, hash_secret},
domain::UserAppSecret,
error::AppError,
repository::{self, RepositoryError},
};
@@ -97,20 +96,9 @@ async fn upsert_user_app_secret(
}
let now = chrono::Utc::now();
if request.expires_at <= now {
return Err(AppError::BadRequest(
"expires_at must be in the future".to_owned(),
));
}
let generated_secret = generate_secret().map_err(|_| AppError::Internal)?;
let secret = UserAppSecret {
id: secret_id,
app_id,
secret_hash: hash_secret(&generated_secret),
expires_at: request.expires_at,
created_at: now,
};
let generated_secret = generate_secret().map_err(|_| AppError::Unknown)?;
let hashed_secret = hash_secret(&generated_secret);
let secret = request.into_domain(secret_id, app_id, hashed_secret, now)?;
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_app_ownership(&mut tx, user_id, app_id).await?;
+1 -1
View File
@@ -96,7 +96,7 @@ async fn upsert_user_app(
.as_ref()
.is_some_and(|previous| previous.user_id != user_id)
{
return Err(AppError::Conflict("User app ID is already in use".into()));
return Err(AppError::AppIdAlreadyExists);
}
tx.commit().await.map_err(RepositoryError::from)?;