From 1deecf32fe814351b2f191c76a12ac8cdd99519d Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Wed, 29 Apr 2026 04:22:02 +0300 Subject: [PATCH] refactor: Replace manual Display and Error implementations with thiserror derive macro in ChargingError - Add thiserror::Error derive to ChargingError enum - Replace manual fmt::Display implementation with #[error] attributes on each variant - Remove manual std::error::Error implementation (provided by thiserror) - Remove std::fmt import as it's no longer needed - Maintain same error message formats and functionality --- apps/charging-engine/src/errors/types.rs | 44 +++++++++++------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/apps/charging-engine/src/errors/types.rs b/apps/charging-engine/src/errors/types.rs index d0b2acd..703601a 100644 --- a/apps/charging-engine/src/errors/types.rs +++ b/apps/charging-engine/src/errors/types.rs @@ -1,43 +1,41 @@ use axum::response::{IntoResponse, Response}; use axum::http::StatusCode; use serde_json::json; -use std::fmt; +use thiserror::Error; -#[derive(Debug, Clone)] +#[derive(Debug, Error, Clone)] pub enum ChargingError { + #[error("Redis connection error: {0}")] RedisConnection(String), + + #[error("Redis operation error: {0}")] RedisOperation(String), + + #[error("Database error: {0}")] DatabaseError(String), + + #[error("Subscriber not found: {0}")] SubscriberNotFound(String), + + #[error("Rating plan not found: {0}")] RatingPlanNotFound(String), + + #[error("Insufficient credit: available={available}, requested={requested}")] InsufficientCredit { available: u64, requested: u64 }, + + #[error("Usage blocked: {0}")] UsageBlocked(String), + + #[error("Invalid input: {0}")] InvalidInput(String), + + #[error("Serialization error: {0}")] SerializationError(String), + + #[error("Internal error: {0}")] InternalError(String), } -impl fmt::Display for ChargingError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - ChargingError::RedisConnection(msg) => write!(f, "Redis connection error: {}", msg), - ChargingError::RedisOperation(msg) => write!(f, "Redis operation error: {}", msg), - ChargingError::DatabaseError(msg) => write!(f, "Database error: {}", msg), - ChargingError::SubscriberNotFound(imsi) => write!(f, "Subscriber not found: {}", imsi), - ChargingError::RatingPlanNotFound(plan_id) => write!(f, "Rating plan not found: {}", plan_id), - ChargingError::InsufficientCredit { available, requested } => { - write!(f, "Insufficient credit: available={}, requested={}", available, requested) - } - ChargingError::UsageBlocked(reason) => write!(f, "Usage blocked: {}", reason), - ChargingError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg), - ChargingError::SerializationError(msg) => write!(f, "Serialization error: {}", msg), - ChargingError::InternalError(msg) => write!(f, "Internal error: {}", msg), - } - } -} - -impl std::error::Error for ChargingError {} - impl IntoResponse for ChargingError { fn into_response(self) -> Response { let (status, message) = match &self {