Cleaned up errors a bit.
This commit is contained in:
@@ -4,23 +4,19 @@ mod recurring_transaction;
|
|||||||
mod schedule;
|
mod schedule;
|
||||||
mod transaction;
|
mod transaction;
|
||||||
|
|
||||||
pub use cents::{Cents, CentsConversionError};
|
pub use cents::{Cents, CentsError};
|
||||||
pub use direction::Direction;
|
pub use direction::Direction;
|
||||||
pub use recurring_transaction::RecurringTransaction;
|
pub use recurring_transaction::RecurringTransaction;
|
||||||
pub use schedule::{
|
pub use schedule::{
|
||||||
Recurrence, RecurrenceInterval, RecurrenceIntervalError, RecurrenceUnit, Schedule,
|
Recurrence, RecurrenceInterval, RecurrenceUnit, Schedule, ScheduleError, WeekendAdjustment,
|
||||||
WeekendAdjustment,
|
|
||||||
};
|
};
|
||||||
pub use transaction::Transaction;
|
pub use transaction::Transaction;
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum ValidationError {
|
pub enum DomainError {
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Amount(#[from] CentsConversionError),
|
Amount(#[from] CentsError),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
RecurrenceInterval(#[from] RecurrenceIntervalError),
|
Schedule(#[from] ScheduleError),
|
||||||
|
|
||||||
#[error("end must not be before start")]
|
|
||||||
EndBeforeStart,
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,22 +9,22 @@ impl Cents {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u64> for Cents {
|
impl TryFrom<u64> for Cents {
|
||||||
type Error = CentsConversionError;
|
type Error = CentsError;
|
||||||
|
|
||||||
fn try_from(value: u64) -> Result<Self, Self::Error> {
|
fn try_from(value: u64) -> Result<Self, Self::Error> {
|
||||||
i64::try_from(value)
|
i64::try_from(value)
|
||||||
.map(|_| Self(value))
|
.map(|_| Self(value))
|
||||||
.map_err(|_| CentsConversionError(value.into()))
|
.map_err(|_| CentsError(value.into()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<i64> for Cents {
|
impl TryFrom<i64> for Cents {
|
||||||
type Error = CentsConversionError;
|
type Error = CentsError;
|
||||||
|
|
||||||
fn try_from(value: i64) -> Result<Self, Self::Error> {
|
fn try_from(value: i64) -> Result<Self, Self::Error> {
|
||||||
u64::try_from(value)
|
u64::try_from(value)
|
||||||
.map(Self)
|
.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)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
#[error("invalid cents value: {0}")]
|
#[error("invalid cents value: {0}")]
|
||||||
pub struct CentsConversionError(pub i128);
|
pub struct CentsError(pub i128);
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{Cents, CentsConversionError};
|
use super::{Cents, CentsError};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn accepts_amounts_up_to_i64_max() {
|
fn accepts_amounts_up_to_i64_max() {
|
||||||
@@ -55,7 +55,7 @@ mod tests {
|
|||||||
fn rejects_amounts_above_i64_max() {
|
fn rejects_amounts_above_i64_max() {
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
Cents::try_from(i64::MAX as u64 + 1),
|
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]
|
#[test]
|
||||||
fn rejects_negative_database_values() {
|
fn rejects_negative_database_values() {
|
||||||
assert!(matches!(
|
assert!(matches!(Cents::try_from(-1_i64), Err(CentsError(-1_i128))));
|
||||||
Cents::try_from(-1_i64),
|
|
||||||
Err(CentsConversionError(-1_i128))
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
use chrono::{DateTime, Datelike, Months, TimeDelta, Utc, Weekday};
|
use chrono::{DateTime, Datelike, Months, TimeDelta, Utc, Weekday};
|
||||||
|
|
||||||
use super::ValidationError;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Schedule {
|
pub struct Schedule {
|
||||||
start: DateTime<Utc>,
|
start: DateTime<Utc>,
|
||||||
@@ -16,9 +14,9 @@ impl Schedule {
|
|||||||
end: Option<DateTime<Utc>>,
|
end: Option<DateTime<Utc>>,
|
||||||
recurrence: Recurrence,
|
recurrence: Recurrence,
|
||||||
weekend_adjustment: Option<WeekendAdjustment>,
|
weekend_adjustment: Option<WeekendAdjustment>,
|
||||||
) -> Result<Self, ValidationError> {
|
) -> Result<Self, ScheduleError> {
|
||||||
if end.is_some_and(|end| end < start) {
|
if end.is_some_and(|end| end < start) {
|
||||||
return Err(ValidationError::EndBeforeStart);
|
return Err(ScheduleError::EndBeforeStart);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@@ -120,25 +118,25 @@ impl RecurrenceInterval {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u32> for RecurrenceInterval {
|
impl TryFrom<u32> for RecurrenceInterval {
|
||||||
type Error = RecurrenceIntervalError;
|
type Error = ScheduleError;
|
||||||
|
|
||||||
fn try_from(value: u32) -> Result<Self, Self::Error> {
|
fn try_from(value: u32) -> Result<Self, Self::Error> {
|
||||||
if (1..=i32::MAX as u32).contains(&value) {
|
if (1..=i32::MAX as u32).contains(&value) {
|
||||||
Ok(Self(value))
|
Ok(Self(value))
|
||||||
} else {
|
} else {
|
||||||
Err(RecurrenceIntervalError(value.into()))
|
Err(ScheduleError::RecurrenceInterval(value.into()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<i32> for RecurrenceInterval {
|
impl TryFrom<i32> for RecurrenceInterval {
|
||||||
type Error = RecurrenceIntervalError;
|
type Error = ScheduleError;
|
||||||
|
|
||||||
fn try_from(value: i32) -> Result<Self, Self::Error> {
|
fn try_from(value: i32) -> Result<Self, Self::Error> {
|
||||||
u32::try_from(value)
|
u32::try_from(value)
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|value| Self::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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum RecurrenceUnit {
|
pub enum RecurrenceUnit {
|
||||||
Day,
|
Day,
|
||||||
@@ -166,15 +160,22 @@ pub enum WeekendAdjustment {
|
|||||||
NextBusinessDay,
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use chrono::{DateTime, TimeDelta, TimeZone, Utc};
|
use chrono::{DateTime, TimeDelta, TimeZone, Utc};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
Recurrence, RecurrenceInterval, RecurrenceIntervalError, RecurrenceUnit, Schedule,
|
Recurrence, RecurrenceInterval, RecurrenceUnit, Schedule, ScheduleError, WeekendAdjustment,
|
||||||
WeekendAdjustment,
|
|
||||||
};
|
};
|
||||||
use crate::domain::ValidationError;
|
|
||||||
|
|
||||||
fn recurrence(every: u32, unit: RecurrenceUnit) -> Recurrence {
|
fn recurrence(every: u32, unit: RecurrenceUnit) -> Recurrence {
|
||||||
Recurrence {
|
Recurrence {
|
||||||
@@ -325,7 +326,7 @@ mod tests {
|
|||||||
|
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
Schedule::new(start, Some(start - TimeDelta::seconds(1)), recurrence, None,),
|
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());
|
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] {
|
for every in [0, i32::MAX as u32 + 1] {
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
RecurrenceInterval::try_from(every),
|
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] {
|
for every in [0_i32, -1] {
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
RecurrenceInterval::try_from(every),
|
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},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{domain::ValidationError, repository::RepositoryError};
|
use crate::{domain::DomainError, repository::RepositoryError};
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum AppError {
|
pub enum AppError {
|
||||||
@@ -13,11 +13,11 @@ pub enum AppError {
|
|||||||
#[error("not found")]
|
#[error("not found")]
|
||||||
NotFound,
|
NotFound,
|
||||||
|
|
||||||
|
#[error("domain error")]
|
||||||
|
Domain(#[from] DomainError),
|
||||||
|
|
||||||
#[error("repository error")]
|
#[error("repository error")]
|
||||||
Repository(#[from] RepositoryError),
|
Repository(#[from] RepositoryError),
|
||||||
|
|
||||||
#[error("validation error")]
|
|
||||||
Validation(#[from] ValidationError),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for AppError {
|
impl IntoResponse for AppError {
|
||||||
@@ -29,6 +29,9 @@ impl IntoResponse for AppError {
|
|||||||
Self::NotFound => {
|
Self::NotFound => {
|
||||||
return error_response(StatusCode::NOT_FOUND, "Not found");
|
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)) => {
|
Self::Repository(RepositoryError::Database(error)) => {
|
||||||
tracing::error!(%error, "Database query failed");
|
tracing::error!(%error, "Database query failed");
|
||||||
}
|
}
|
||||||
@@ -38,9 +41,6 @@ impl IntoResponse for AppError {
|
|||||||
Self::Repository(RepositoryError::QueryConversion(error)) => {
|
Self::Repository(RepositoryError::QueryConversion(error)) => {
|
||||||
return error_response(StatusCode::BAD_REQUEST, error.to_string());
|
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")
|
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;
|
pub mod dtos;
|
||||||
mod encoding;
|
mod encoding;
|
||||||
@@ -20,13 +20,7 @@ pub enum RepositoryError {
|
|||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum DecodeError {
|
pub enum DecodeError {
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Amount(#[from] CentsConversionError),
|
Amount(#[from] CentsError),
|
||||||
|
|
||||||
#[error(transparent)]
|
|
||||||
RecurrenceInterval(#[from] RecurrenceIntervalError),
|
|
||||||
|
|
||||||
#[error(transparent)]
|
|
||||||
Invalid(#[from] ValidationError),
|
|
||||||
|
|
||||||
#[error("invalid direction value: {0}")]
|
#[error("invalid direction value: {0}")]
|
||||||
Direction(String),
|
Direction(String),
|
||||||
@@ -34,6 +28,9 @@ pub enum DecodeError {
|
|||||||
#[error("invalid recurrence_unit value: {0}")]
|
#[error("invalid recurrence_unit value: {0}")]
|
||||||
RecurrenceUnit(String),
|
RecurrenceUnit(String),
|
||||||
|
|
||||||
|
#[error(transparent)]
|
||||||
|
Schedule(#[from] ScheduleError),
|
||||||
|
|
||||||
#[error("invalid weekend_adjustment value: {0}")]
|
#[error("invalid weekend_adjustment value: {0}")]
|
||||||
WeekendAdjustment(String),
|
WeekendAdjustment(String),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ mod tests {
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::domain::{Direction, RecurrenceUnit, ValidationError, WeekendAdjustment};
|
use crate::domain::{Direction, RecurrenceUnit, ScheduleError, WeekendAdjustment};
|
||||||
|
|
||||||
fn row() -> RecurringTransactionRow {
|
fn row() -> RecurringTransactionRow {
|
||||||
RecurringTransactionRow {
|
RecurringTransactionRow {
|
||||||
@@ -171,7 +171,7 @@ mod tests {
|
|||||||
every.recurrence_every = 0;
|
every.recurrence_every = 0;
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
decode(every),
|
decode(every),
|
||||||
Err(DecodeError::RecurrenceInterval(_))
|
Err(DecodeError::Schedule(ScheduleError::RecurrenceInterval(_)))
|
||||||
));
|
));
|
||||||
|
|
||||||
let mut unit = row();
|
let mut unit = row();
|
||||||
@@ -182,7 +182,7 @@ mod tests {
|
|||||||
end.ends_at = Some(end.starts_at - TimeDelta::seconds(1));
|
end.ends_at = Some(end.starts_at - TimeDelta::seconds(1));
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
decode(end),
|
decode(end),
|
||||||
Err(DecodeError::Invalid(ValidationError::EndBeforeStart))
|
Err(DecodeError::Schedule(ScheduleError::EndBeforeStart))
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ impl RecurringTransactionRequest {
|
|||||||
pub fn into_domain(
|
pub fn into_domain(
|
||||||
self,
|
self,
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
) -> Result<domain::RecurringTransaction, domain::ValidationError> {
|
) -> Result<domain::RecurringTransaction, domain::DomainError> {
|
||||||
let recurrence = domain::Recurrence {
|
let recurrence = domain::Recurrence {
|
||||||
every: domain::RecurrenceInterval::try_from(self.schedule.recurrence.every)?,
|
every: domain::RecurrenceInterval::try_from(self.schedule.recurrence.every)?,
|
||||||
unit: self.schedule.recurrence.unit.into(),
|
unit: self.schedule.recurrence.unit.into(),
|
||||||
@@ -189,7 +189,10 @@ mod tests {
|
|||||||
ScheduleRequest, ScheduledTransactionRequest, ScheduledTransactionResponse,
|
ScheduleRequest, ScheduledTransactionRequest, ScheduledTransactionResponse,
|
||||||
WeekendAdjustment,
|
WeekendAdjustment,
|
||||||
};
|
};
|
||||||
use crate::{domain, routes::dtos::direction::Direction};
|
use crate::{
|
||||||
|
domain::{self, ScheduleError},
|
||||||
|
routes::dtos::direction::Direction,
|
||||||
|
};
|
||||||
|
|
||||||
fn request(amount_cents: u64) -> RecurringTransactionRequest {
|
fn request(amount_cents: u64) -> RecurringTransactionRequest {
|
||||||
RecurringTransactionRequest {
|
RecurringTransactionRequest {
|
||||||
@@ -256,21 +259,23 @@ mod tests {
|
|||||||
fn rejects_invalid_requests() {
|
fn rejects_invalid_requests() {
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
request(i64::MAX as u64 + 1).into_domain(Uuid::nil()),
|
request(i64::MAX as u64 + 1).into_domain(Uuid::nil()),
|
||||||
Err(domain::ValidationError::Amount(_))
|
Err(domain::DomainError::Amount(_))
|
||||||
));
|
));
|
||||||
|
|
||||||
let mut every = request(100);
|
let mut every = request(100);
|
||||||
every.schedule.recurrence.every = 0;
|
every.schedule.recurrence.every = 0;
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
every.into_domain(Uuid::nil()),
|
every.into_domain(Uuid::nil()),
|
||||||
Err(domain::ValidationError::RecurrenceInterval(_))
|
Err(domain::DomainError::Schedule(
|
||||||
|
ScheduleError::RecurrenceInterval(_)
|
||||||
|
))
|
||||||
));
|
));
|
||||||
|
|
||||||
let mut end = request(100);
|
let mut end = request(100);
|
||||||
end.schedule.end = Some(end.schedule.start - TimeDelta::seconds(1));
|
end.schedule.end = Some(end.schedule.start - TimeDelta::seconds(1));
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
end.into_domain(Uuid::nil()),
|
end.into_domain(Uuid::nil()),
|
||||||
Err(domain::ValidationError::EndBeforeStart)
|
Err(domain::DomainError::Schedule(ScheduleError::EndBeforeStart))
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ pub struct TransactionRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
Ok(domain::Transaction {
|
||||||
id,
|
id,
|
||||||
description: self.description,
|
description: self.description,
|
||||||
@@ -80,7 +80,7 @@ mod tests {
|
|||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
error,
|
error,
|
||||||
domain::ValidationError::Amount(domain::CentsConversionError(value))
|
domain::DomainError::Amount(domain::CentsError(value))
|
||||||
if value == i64::MAX as i128 + 1
|
if value == i64::MAX as i128 + 1
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user