diff --git a/server/src/domain.rs b/server/src/domain.rs index a82af3a..c7a2462 100644 --- a/server/src/domain.rs +++ b/server/src/domain.rs @@ -4,23 +4,19 @@ mod recurring_transaction; mod schedule; mod transaction; -pub use cents::{Cents, CentsConversionError}; +pub use cents::{Cents, CentsError}; pub use direction::Direction; pub use recurring_transaction::RecurringTransaction; pub use schedule::{ - Recurrence, RecurrenceInterval, RecurrenceIntervalError, RecurrenceUnit, Schedule, - WeekendAdjustment, + Recurrence, RecurrenceInterval, RecurrenceUnit, Schedule, ScheduleError, WeekendAdjustment, }; pub use transaction::Transaction; #[derive(Debug, thiserror::Error)] -pub enum ValidationError { +pub enum DomainError { #[error(transparent)] - Amount(#[from] CentsConversionError), + Amount(#[from] CentsError), #[error(transparent)] - RecurrenceInterval(#[from] RecurrenceIntervalError), - - #[error("end must not be before start")] - EndBeforeStart, + Schedule(#[from] ScheduleError), } diff --git a/server/src/domain/cents.rs b/server/src/domain/cents.rs index 94d40e9..f4ff1a7 100644 --- a/server/src/domain/cents.rs +++ b/server/src/domain/cents.rs @@ -9,22 +9,22 @@ impl Cents { } impl TryFrom for Cents { - type Error = CentsConversionError; + type Error = CentsError; fn try_from(value: u64) -> Result { i64::try_from(value) .map(|_| Self(value)) - .map_err(|_| CentsConversionError(value.into())) + .map_err(|_| CentsError(value.into())) } } impl TryFrom for Cents { - type Error = CentsConversionError; + type Error = CentsError; fn try_from(value: i64) -> Result { u64::try_from(value) .map(Self) - .map_err(|_| CentsConversionError(value.into())) + .map_err(|_| CentsError(value.into())) } } @@ -36,11 +36,11 @@ impl From for i64 { #[derive(Debug, thiserror::Error)] #[error("invalid cents value: {0}")] -pub struct CentsConversionError(pub i128); +pub struct CentsError(pub i128); #[cfg(test)] mod tests { - use super::{Cents, CentsConversionError}; + use super::{Cents, CentsError}; #[test] fn accepts_amounts_up_to_i64_max() { @@ -55,7 +55,7 @@ mod tests { fn rejects_amounts_above_i64_max() { assert!(matches!( Cents::try_from(i64::MAX as u64 + 1), - Err(CentsConversionError(value)) if value == i64::MAX as i128 + 1 + Err(CentsError(value)) if value == i64::MAX as i128 + 1 )); } @@ -66,9 +66,6 @@ mod tests { #[test] fn rejects_negative_database_values() { - assert!(matches!( - Cents::try_from(-1_i64), - Err(CentsConversionError(-1_i128)) - )); + assert!(matches!(Cents::try_from(-1_i64), Err(CentsError(-1_i128)))); } } diff --git a/server/src/domain/schedule.rs b/server/src/domain/schedule.rs index bc7aac3..4198664 100644 --- a/server/src/domain/schedule.rs +++ b/server/src/domain/schedule.rs @@ -1,7 +1,5 @@ use chrono::{DateTime, Datelike, Months, TimeDelta, Utc, Weekday}; -use super::ValidationError; - #[derive(Debug, Clone, PartialEq, Eq)] pub struct Schedule { start: DateTime, @@ -16,9 +14,9 @@ impl Schedule { end: Option>, recurrence: Recurrence, weekend_adjustment: Option, - ) -> Result { + ) -> Result { if end.is_some_and(|end| end < start) { - return Err(ValidationError::EndBeforeStart); + return Err(ScheduleError::EndBeforeStart); } Ok(Self { @@ -120,25 +118,25 @@ impl RecurrenceInterval { } impl TryFrom for RecurrenceInterval { - type Error = RecurrenceIntervalError; + type Error = ScheduleError; fn try_from(value: u32) -> Result { if (1..=i32::MAX as u32).contains(&value) { Ok(Self(value)) } else { - Err(RecurrenceIntervalError(value.into())) + Err(ScheduleError::RecurrenceInterval(value.into())) } } } impl TryFrom for RecurrenceInterval { - type Error = RecurrenceIntervalError; + type Error = ScheduleError; fn try_from(value: i32) -> Result { u32::try_from(value) .ok() .and_then(|value| Self::try_from(value).ok()) - .ok_or(RecurrenceIntervalError(value.into())) + .ok_or(ScheduleError::RecurrenceInterval(value.into())) } } @@ -148,10 +146,6 @@ impl From for i32 { } } -#[derive(Debug, thiserror::Error)] -#[error("recurrence every must be between 1 and {}: {}", i32::MAX, .0)] -pub struct RecurrenceIntervalError(pub i64); - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum RecurrenceUnit { Day, @@ -166,15 +160,22 @@ pub enum WeekendAdjustment { NextBusinessDay, } +#[derive(Debug, thiserror::Error)] +pub enum ScheduleError { + #[error("recurrence every must be between 1 and {}: {}", i32::MAX, .0)] + RecurrenceInterval(i64), + + #[error("end must not be before start")] + EndBeforeStart, +} + #[cfg(test)] mod tests { use chrono::{DateTime, TimeDelta, TimeZone, Utc}; use super::{ - Recurrence, RecurrenceInterval, RecurrenceIntervalError, RecurrenceUnit, Schedule, - WeekendAdjustment, + Recurrence, RecurrenceInterval, RecurrenceUnit, Schedule, ScheduleError, WeekendAdjustment, }; - use crate::domain::ValidationError; fn recurrence(every: u32, unit: RecurrenceUnit) -> Recurrence { Recurrence { @@ -325,7 +326,7 @@ mod tests { assert!(matches!( Schedule::new(start, Some(start - TimeDelta::seconds(1)), recurrence, None,), - Err(ValidationError::EndBeforeStart) + Err(ScheduleError::EndBeforeStart) )); assert!(Schedule::new(start, Some(start), recurrence, None).is_ok()); } @@ -340,13 +341,13 @@ mod tests { for every in [0, i32::MAX as u32 + 1] { assert!(matches!( RecurrenceInterval::try_from(every), - Err(RecurrenceIntervalError(value)) if value == i64::from(every) + Err(ScheduleError::RecurrenceInterval(value)) if value == i64::from(every) )); } for every in [0_i32, -1] { assert!(matches!( RecurrenceInterval::try_from(every), - Err(RecurrenceIntervalError(value)) if value == i64::from(every) + Err(ScheduleError::RecurrenceInterval(value)) if value == i64::from(every) )); } } diff --git a/server/src/error.rs b/server/src/error.rs index 506a6ac..417b351 100644 --- a/server/src/error.rs +++ b/server/src/error.rs @@ -3,7 +3,7 @@ use axum::{ response::{IntoResponse, Response}, }; -use crate::{domain::ValidationError, repository::RepositoryError}; +use crate::{domain::DomainError, repository::RepositoryError}; #[derive(Debug, thiserror::Error)] pub enum AppError { @@ -13,11 +13,11 @@ pub enum AppError { #[error("not found")] NotFound, + #[error("domain error")] + Domain(#[from] DomainError), + #[error("repository error")] Repository(#[from] RepositoryError), - - #[error("validation error")] - Validation(#[from] ValidationError), } impl IntoResponse for AppError { @@ -29,6 +29,9 @@ impl IntoResponse for AppError { Self::NotFound => { return error_response(StatusCode::NOT_FOUND, "Not found"); } + Self::Domain(error) => { + return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string()); + } Self::Repository(RepositoryError::Database(error)) => { tracing::error!(%error, "Database query failed"); } @@ -38,9 +41,6 @@ impl IntoResponse for AppError { Self::Repository(RepositoryError::QueryConversion(error)) => { return error_response(StatusCode::BAD_REQUEST, error.to_string()); } - Self::Validation(error) => { - return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string()); - } } error_response(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error") diff --git a/server/src/repository.rs b/server/src/repository.rs index d5c5e0d..3bbf9ea 100644 --- a/server/src/repository.rs +++ b/server/src/repository.rs @@ -1,4 +1,4 @@ -use crate::domain::{CentsConversionError, RecurrenceIntervalError, ValidationError}; +use crate::domain::{CentsError, ScheduleError}; pub mod dtos; mod encoding; @@ -20,13 +20,7 @@ pub enum RepositoryError { #[derive(Debug, thiserror::Error)] pub enum DecodeError { #[error(transparent)] - Amount(#[from] CentsConversionError), - - #[error(transparent)] - RecurrenceInterval(#[from] RecurrenceIntervalError), - - #[error(transparent)] - Invalid(#[from] ValidationError), + Amount(#[from] CentsError), #[error("invalid direction value: {0}")] Direction(String), @@ -34,6 +28,9 @@ pub enum DecodeError { #[error("invalid recurrence_unit value: {0}")] RecurrenceUnit(String), + #[error(transparent)] + Schedule(#[from] ScheduleError), + #[error("invalid weekend_adjustment value: {0}")] WeekendAdjustment(String), } diff --git a/server/src/repository/recurring_transactions.rs b/server/src/repository/recurring_transactions.rs index aaa414b..f35e659 100644 --- a/server/src/repository/recurring_transactions.rs +++ b/server/src/repository/recurring_transactions.rs @@ -128,7 +128,7 @@ mod tests { use uuid::Uuid; use super::*; - use crate::domain::{Direction, RecurrenceUnit, ValidationError, WeekendAdjustment}; + use crate::domain::{Direction, RecurrenceUnit, ScheduleError, WeekendAdjustment}; fn row() -> RecurringTransactionRow { RecurringTransactionRow { @@ -171,7 +171,7 @@ mod tests { every.recurrence_every = 0; assert!(matches!( decode(every), - Err(DecodeError::RecurrenceInterval(_)) + Err(DecodeError::Schedule(ScheduleError::RecurrenceInterval(_))) )); let mut unit = row(); @@ -182,7 +182,7 @@ mod tests { end.ends_at = Some(end.starts_at - TimeDelta::seconds(1)); assert!(matches!( decode(end), - Err(DecodeError::Invalid(ValidationError::EndBeforeStart)) + Err(DecodeError::Schedule(ScheduleError::EndBeforeStart)) )); } diff --git a/server/src/routes/dtos/scheduled_transaction.rs b/server/src/routes/dtos/scheduled_transaction.rs index 386f9ff..9cd8908 100644 --- a/server/src/routes/dtos/scheduled_transaction.rs +++ b/server/src/routes/dtos/scheduled_transaction.rs @@ -37,7 +37,7 @@ impl RecurringTransactionRequest { pub fn into_domain( self, id: Uuid, - ) -> Result { + ) -> Result { let recurrence = domain::Recurrence { every: domain::RecurrenceInterval::try_from(self.schedule.recurrence.every)?, unit: self.schedule.recurrence.unit.into(), @@ -189,7 +189,10 @@ mod tests { ScheduleRequest, ScheduledTransactionRequest, ScheduledTransactionResponse, WeekendAdjustment, }; - use crate::{domain, routes::dtos::direction::Direction}; + use crate::{ + domain::{self, ScheduleError}, + routes::dtos::direction::Direction, + }; fn request(amount_cents: u64) -> RecurringTransactionRequest { RecurringTransactionRequest { @@ -256,21 +259,23 @@ mod tests { fn rejects_invalid_requests() { assert!(matches!( request(i64::MAX as u64 + 1).into_domain(Uuid::nil()), - Err(domain::ValidationError::Amount(_)) + Err(domain::DomainError::Amount(_)) )); let mut every = request(100); every.schedule.recurrence.every = 0; assert!(matches!( every.into_domain(Uuid::nil()), - Err(domain::ValidationError::RecurrenceInterval(_)) + Err(domain::DomainError::Schedule( + ScheduleError::RecurrenceInterval(_) + )) )); let mut end = request(100); end.schedule.end = Some(end.schedule.start - TimeDelta::seconds(1)); assert!(matches!( end.into_domain(Uuid::nil()), - Err(domain::ValidationError::EndBeforeStart) + Err(domain::DomainError::Schedule(ScheduleError::EndBeforeStart)) )); } } diff --git a/server/src/routes/dtos/transaction.rs b/server/src/routes/dtos/transaction.rs index 5d49e55..92338ec 100644 --- a/server/src/routes/dtos/transaction.rs +++ b/server/src/routes/dtos/transaction.rs @@ -14,7 +14,7 @@ pub struct TransactionRequest { } impl TransactionRequest { - pub fn into_domain(self, id: Uuid) -> Result { + pub fn into_domain(self, id: Uuid) -> Result { Ok(domain::Transaction { id, description: self.description, @@ -80,7 +80,7 @@ mod tests { .unwrap_err(); assert!(matches!( error, - domain::ValidationError::Amount(domain::CentsConversionError(value)) + domain::DomainError::Amount(domain::CentsError(value)) if value == i64::MAX as i128 + 1 )); }