mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-27 23:22:15 +00:00
feat: Add ErrorContext trait implementation for anyhow::Result to enable context error handling with ChargingError conversion
- Add ChargingError import from super::types - Add ErrorContext<T> implementation for anyhow::Result<T> with with_context method that maps errors to ChargingError::InternalError with formatted context message
This commit is contained in:
@@ -1,11 +1,20 @@
|
||||
use anyhow::Context;
|
||||
|
||||
use super::types::ChargingError;
|
||||
|
||||
pub type ChargingResult<T> = Result<T, ChargingError>;
|
||||
|
||||
pub trait ErrorContext<T> {
|
||||
fn with_context(self, msg: &str) -> ChargingResult<T>;
|
||||
}
|
||||
|
||||
impl<T> ErrorContext<T> for anyhow::Result<T> {
|
||||
fn with_context(self, msg: &str) -> ChargingResult<T> {
|
||||
self.map_err(|e| ChargingError::InternalError(format!("{}: {}", msg, e)))
|
||||
.map_err(|e| e.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E> ErrorContext<T> for Result<T, E>
|
||||
where
|
||||
E: Into<ChargingError>,
|
||||
|
||||
Reference in New Issue
Block a user