mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-12-01 16:28:48 +00:00
feat: Add DatabaseError variant and improve error response handling
- Add DatabaseError(String) variant to ChargingError enum - Add DatabaseError case to log_error helper with error-level logging - Add DatabaseError Display implementation - Refactor IntoResponse to use generic error messages instead of detailed strings - Add detail field to error responses containing full error message via to_string() - Change RedisConnection status code from INTERNAL_SERVER_ERROR to SERVICE_UNAVAILABLE
This commit is contained in:
@@ -126,6 +126,9 @@ pub fn log_error(error: &ChargingError) {
|
|||||||
ChargingError::RedisOperation(msg) => {
|
ChargingError::RedisOperation(msg) => {
|
||||||
tracing::error!("Redis operation error: {}", msg);
|
tracing::error!("Redis operation error: {}", msg);
|
||||||
}
|
}
|
||||||
|
ChargingError::DatabaseError(msg) => {
|
||||||
|
tracing::error!("Database error: {}", msg);
|
||||||
|
}
|
||||||
ChargingError::SubscriberNotFound(imsi) => {
|
ChargingError::SubscriberNotFound(imsi) => {
|
||||||
tracing::warn!("Subscriber not found: {}", imsi);
|
tracing::warn!("Subscriber not found: {}", imsi);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use std::fmt;
|
|||||||
pub enum ChargingError {
|
pub enum ChargingError {
|
||||||
RedisConnection(String),
|
RedisConnection(String),
|
||||||
RedisOperation(String),
|
RedisOperation(String),
|
||||||
|
DatabaseError(String),
|
||||||
SubscriberNotFound(String),
|
SubscriberNotFound(String),
|
||||||
RatingPlanNotFound(String),
|
RatingPlanNotFound(String),
|
||||||
InsufficientCredit { available: u64, requested: u64 },
|
InsufficientCredit { available: u64, requested: u64 },
|
||||||
@@ -21,6 +22,7 @@ impl fmt::Display for ChargingError {
|
|||||||
match self {
|
match self {
|
||||||
ChargingError::RedisConnection(msg) => write!(f, "Redis connection error: {}", msg),
|
ChargingError::RedisConnection(msg) => write!(f, "Redis connection error: {}", msg),
|
||||||
ChargingError::RedisOperation(msg) => write!(f, "Redis operation 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::SubscriberNotFound(imsi) => write!(f, "Subscriber not found: {}", imsi),
|
||||||
ChargingError::RatingPlanNotFound(plan_id) => write!(f, "Rating plan not found: {}", plan_id),
|
ChargingError::RatingPlanNotFound(plan_id) => write!(f, "Rating plan not found: {}", plan_id),
|
||||||
ChargingError::InsufficientCredit { available, requested } => {
|
ChargingError::InsufficientCredit { available, requested } => {
|
||||||
@@ -38,22 +40,22 @@ impl std::error::Error for ChargingError {}
|
|||||||
|
|
||||||
impl IntoResponse for ChargingError {
|
impl IntoResponse for ChargingError {
|
||||||
fn into_response(self) -> Response {
|
fn into_response(self) -> Response {
|
||||||
let (status, error_message) = match self {
|
let (status, message) = match &self {
|
||||||
ChargingError::RedisConnection(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
|
ChargingError::RedisConnection(_) => (StatusCode::SERVICE_UNAVAILABLE, "Redis connection error"),
|
||||||
ChargingError::RedisOperation(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
|
ChargingError::RedisOperation(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Redis operation error"),
|
||||||
ChargingError::SubscriberNotFound(imsi) => (StatusCode::NOT_FOUND, format!("Subscriber not found: {}", imsi)),
|
ChargingError::DatabaseError(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Database error"),
|
||||||
ChargingError::RatingPlanNotFound(plan_id) => (StatusCode::NOT_FOUND, format!("Rating plan not found: {}", plan_id)),
|
ChargingError::SubscriberNotFound(_) => (StatusCode::NOT_FOUND, "Subscriber not found"),
|
||||||
ChargingError::InsufficientCredit { available, requested } => {
|
ChargingError::RatingPlanNotFound(_) => (StatusCode::NOT_FOUND, "Rating plan not found"),
|
||||||
(StatusCode::PAYMENT_REQUIRED, format!("Insufficient credit: available={}, requested={}", available, requested))
|
ChargingError::InsufficientCredit { .. } => (StatusCode::PAYMENT_REQUIRED, "Insufficient credit"),
|
||||||
}
|
ChargingError::UsageBlocked(_) => (StatusCode::FORBIDDEN, "Usage blocked"),
|
||||||
ChargingError::UsageBlocked(reason) => (StatusCode::FORBIDDEN, reason),
|
ChargingError::InvalidInput(_) => (StatusCode::BAD_REQUEST, "Invalid input"),
|
||||||
ChargingError::InvalidInput(msg) => (StatusCode::BAD_REQUEST, msg),
|
ChargingError::SerializationError(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Serialization error"),
|
||||||
ChargingError::SerializationError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
|
ChargingError::InternalError(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Internal error"),
|
||||||
ChargingError::InternalError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let body = json!({
|
let body = json!({
|
||||||
"error": error_message
|
"error": message,
|
||||||
|
"detail": self.to_string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
(status, axum::Json(body)).into_response()
|
(status, axum::Json(body)).into_response()
|
||||||
|
|||||||
Reference in New Issue
Block a user