Cleaned up errors a bit.
This commit is contained in:
@@ -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),
|
||||
}
|
||||
|
||||
@@ -9,22 +9,22 @@ impl Cents {
|
||||
}
|
||||
|
||||
impl TryFrom<u64> for Cents {
|
||||
type Error = CentsConversionError;
|
||||
type Error = CentsError;
|
||||
|
||||
fn try_from(value: u64) -> Result<Self, Self::Error> {
|
||||
i64::try_from(value)
|
||||
.map(|_| Self(value))
|
||||
.map_err(|_| CentsConversionError(value.into()))
|
||||
.map_err(|_| CentsError(value.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<i64> for Cents {
|
||||
type Error = CentsConversionError;
|
||||
type Error = CentsError;
|
||||
|
||||
fn try_from(value: i64) -> Result<Self, Self::Error> {
|
||||
u64::try_from(value)
|
||||
.map(Self)
|
||||
.map_err(|_| CentsConversionError(value.into()))
|
||||
.map_err(|_| CentsError(value.into()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,11 +36,11 @@ impl From<Cents> 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))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Utc>,
|
||||
@@ -16,9 +14,9 @@ impl Schedule {
|
||||
end: Option<DateTime<Utc>>,
|
||||
recurrence: Recurrence,
|
||||
weekend_adjustment: Option<WeekendAdjustment>,
|
||||
) -> Result<Self, ValidationError> {
|
||||
) -> Result<Self, ScheduleError> {
|
||||
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<u32> for RecurrenceInterval {
|
||||
type Error = RecurrenceIntervalError;
|
||||
type Error = ScheduleError;
|
||||
|
||||
fn try_from(value: u32) -> Result<Self, Self::Error> {
|
||||
if (1..=i32::MAX as u32).contains(&value) {
|
||||
Ok(Self(value))
|
||||
} else {
|
||||
Err(RecurrenceIntervalError(value.into()))
|
||||
Err(ScheduleError::RecurrenceInterval(value.into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<i32> for RecurrenceInterval {
|
||||
type Error = RecurrenceIntervalError;
|
||||
type Error = ScheduleError;
|
||||
|
||||
fn try_from(value: i32) -> Result<Self, Self::Error> {
|
||||
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<RecurrenceInterval> 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)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -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")
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ impl RecurringTransactionRequest {
|
||||
pub fn into_domain(
|
||||
self,
|
||||
id: Uuid,
|
||||
) -> Result<domain::RecurringTransaction, domain::ValidationError> {
|
||||
) -> Result<domain::RecurringTransaction, domain::DomainError> {
|
||||
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))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ pub struct TransactionRequest {
|
||||
}
|
||||
|
||||
impl TransactionRequest {
|
||||
pub fn into_domain(self, id: Uuid) -> Result<domain::Transaction, domain::ValidationError> {
|
||||
pub fn into_domain(self, id: Uuid) -> Result<domain::Transaction, domain::DomainError> {
|
||||
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
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user