Implemented thiserror.
This commit is contained in:
+11
-32
@@ -12,43 +12,22 @@ use crate::routes::{
|
||||
validation::scheduled_transaction::ScheduledTransactionValidationError,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AppError {
|
||||
Database(sqlx::Error),
|
||||
Direction(DirectionConversionError),
|
||||
RecurringTransaction(RecurringTransactionConversionError),
|
||||
Transaction(TransactionConversionError),
|
||||
ScheduledTransactionValidation(ScheduledTransactionValidationError),
|
||||
}
|
||||
#[error("database error")]
|
||||
Database(#[from] sqlx::Error),
|
||||
|
||||
impl From<sqlx::Error> for AppError {
|
||||
fn from(error: sqlx::Error) -> Self {
|
||||
Self::Database(error)
|
||||
}
|
||||
}
|
||||
#[error("direction conversion error")]
|
||||
Direction(#[from] DirectionConversionError),
|
||||
|
||||
impl From<DirectionConversionError> for AppError {
|
||||
fn from(error: DirectionConversionError) -> Self {
|
||||
Self::Direction(error)
|
||||
}
|
||||
}
|
||||
#[error("recurring transaction conversion error")]
|
||||
RecurringTransaction(#[from] RecurringTransactionConversionError),
|
||||
|
||||
impl From<RecurringTransactionConversionError> for AppError {
|
||||
fn from(error: RecurringTransactionConversionError) -> Self {
|
||||
Self::RecurringTransaction(error)
|
||||
}
|
||||
}
|
||||
#[error("transaction conversion error")]
|
||||
Transaction(#[from] TransactionConversionError),
|
||||
|
||||
impl From<TransactionConversionError> for AppError {
|
||||
fn from(error: TransactionConversionError) -> Self {
|
||||
Self::Transaction(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ScheduledTransactionValidationError> for AppError {
|
||||
fn from(error: ScheduledTransactionValidationError) -> Self {
|
||||
Self::ScheduledTransactionValidation(error)
|
||||
}
|
||||
#[error("scheduled transaction validation error")]
|
||||
ScheduledTransactionValidation(#[from] ScheduledTransactionValidationError),
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
|
||||
@@ -28,13 +28,6 @@ impl TryFrom<String> for Direction {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("invalid direction value: {0}")]
|
||||
pub struct DirectionConversionError(pub String);
|
||||
|
||||
impl std::error::Error for DirectionConversionError {}
|
||||
|
||||
impl std::fmt::Display for DirectionConversionError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(formatter, "invalid direction value: {}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,37 +146,24 @@ impl TryFrom<String> for WeekendAdjustment {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RecurringTransactionConversionError {
|
||||
#[error("invalid amount_cents value: {0}")]
|
||||
AmountCents(i64),
|
||||
Direction(DirectionConversionError),
|
||||
|
||||
#[error(transparent)]
|
||||
Direction(#[from] DirectionConversionError),
|
||||
|
||||
#[error("invalid recurrence_every value: {0}")]
|
||||
RecurrenceEvery(i32),
|
||||
|
||||
#[error("invalid recurrence_unit value: {0}")]
|
||||
RecurrenceUnit(String),
|
||||
|
||||
#[error("invalid weekend_adjustment value: {0}")]
|
||||
WeekendAdjustment(String),
|
||||
}
|
||||
|
||||
impl std::error::Error for RecurringTransactionConversionError {}
|
||||
|
||||
impl std::fmt::Display for RecurringTransactionConversionError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::AmountCents(value) => {
|
||||
write!(formatter, "invalid amount_cents value: {value}")
|
||||
}
|
||||
Self::Direction(error) => error.fmt(formatter),
|
||||
Self::RecurrenceEvery(value) => {
|
||||
write!(formatter, "invalid recurrence_every value: {value}")
|
||||
}
|
||||
Self::RecurrenceUnit(value) => {
|
||||
write!(formatter, "invalid recurrence_unit value: {value}")
|
||||
}
|
||||
Self::WeekendAdjustment(value) => {
|
||||
write!(formatter, "invalid weekend_adjustment value: {value}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{TimeZone, Utc};
|
||||
|
||||
@@ -32,23 +32,13 @@ impl TryFrom<TransactionRow> for TransactionResponse {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum TransactionConversionError {
|
||||
#[error("invalid amount_cents value: {0}")]
|
||||
AmountCents(i64),
|
||||
Direction(DirectionConversionError),
|
||||
}
|
||||
|
||||
impl std::error::Error for TransactionConversionError {}
|
||||
|
||||
impl std::fmt::Display for TransactionConversionError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::AmountCents(value) => {
|
||||
write!(formatter, "invalid amount_cents value: {value}")
|
||||
}
|
||||
Self::Direction(error) => error.fmt(formatter),
|
||||
}
|
||||
}
|
||||
#[error(transparent)]
|
||||
Direction(#[from] DirectionConversionError),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -82,31 +82,18 @@ impl TryFrom<u32> for PositiveRecurrenceInterval {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ScheduledTransactionValidationError {
|
||||
#[error("amount is too large")]
|
||||
AmountOutOfRange,
|
||||
|
||||
#[error("recurrence every must be between 1 and {}: {}", i32::MAX, .0)]
|
||||
RecurrenceEvery(u32),
|
||||
|
||||
#[error("end must not be before start")]
|
||||
EndBeforeStart,
|
||||
}
|
||||
|
||||
impl std::error::Error for ScheduledTransactionValidationError {}
|
||||
|
||||
impl std::fmt::Display for ScheduledTransactionValidationError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::AmountOutOfRange => write!(formatter, "amount is too large"),
|
||||
Self::RecurrenceEvery(value) => {
|
||||
write!(
|
||||
formatter,
|
||||
"recurrence every must be between 1 and {}: {value}",
|
||||
i32::MAX
|
||||
)
|
||||
}
|
||||
Self::EndBeforeStart => write!(formatter, "end must not be before start"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{TimeZone, Utc};
|
||||
|
||||
Reference in New Issue
Block a user