feat(status): add error context
This commit was merged in pull request #70.
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
pub type Result<T> = std::result::Result<T, Status>;
|
||||
|
||||
pub trait ResultExt<T> {
|
||||
fn with_context<C, F>(self, context: F) -> Result<T>
|
||||
where
|
||||
C: Into<String>,
|
||||
F: FnOnce() -> C;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Code {
|
||||
InvalidArgument,
|
||||
@@ -48,6 +55,10 @@ impl Status {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_context(self, context: impl Into<String>) -> Self {
|
||||
Self::new(self.code, context).with_source(self)
|
||||
}
|
||||
|
||||
pub fn code(&self) -> Code {
|
||||
self.code
|
||||
}
|
||||
@@ -82,3 +93,16 @@ impl From<std::io::Error> for Status {
|
||||
Self::new(code, error.to_string()).with_source(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E> ResultExt<T> for std::result::Result<T, E>
|
||||
where
|
||||
E: Into<Status>,
|
||||
{
|
||||
fn with_context<C, F>(self, context: F) -> Result<T>
|
||||
where
|
||||
C: Into<String>,
|
||||
F: FnOnce() -> C,
|
||||
{
|
||||
self.map_err(|error| error.into().with_context(context()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user