mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-12-03 01:08:45 +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 anyhow::Context;
|
||||||
|
|
||||||
|
use super::types::ChargingError;
|
||||||
|
|
||||||
pub type ChargingResult<T> = Result<T, ChargingError>;
|
pub type ChargingResult<T> = Result<T, ChargingError>;
|
||||||
|
|
||||||
pub trait ErrorContext<T> {
|
pub trait ErrorContext<T> {
|
||||||
fn with_context(self, msg: &str) -> ChargingResult<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>
|
impl<T, E> ErrorContext<T> for Result<T, E>
|
||||||
where
|
where
|
||||||
E: Into<ChargingError>,
|
E: Into<ChargingError>,
|
||||||
|
|||||||
Reference in New Issue
Block a user