Skip to content
Myr

Add context method to Error

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent b702096 · Browse files · 2 files+20 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+20 added−13 removed

  • other:+20 added−13 removed

2 files+20 added−13 removed

modified:src/context.rs+8 added-13 removed
27 unmodified lines
28 {29 self.map_err(|error| Error::from(ContextError { error, context }))30 }3132 fn with_context<C, F>(self, f: F) -> Result<T, Error>32 fn with_context<C, F>(self, context: F) -> Result<T, Error>33 where34 C: Display + Send + Sync + 'static,35 F: FnOnce() -> C,36 {37 self.map_err(|error| {38 Error::from(ContextError {39 error,40 context: f(),40 context: context(),41 })42 })43 }44}
2 unmodified lines
47 fn context<C>(self, context: C) -> Result<T, Error>48 where49 C: Display + Send + Sync + 'static,50 {51 self.map_err(|error| Error::from(ContextError { error, context }))51 self.map_err(|error| error.context(context))52 }5354 fn with_context<C, F>(self, f: F) -> Result<T, Error>54 fn with_context<C, F>(self, context: F) -> Result<T, Error>55 where56 C: Display + Send + Sync + 'static,57 F: FnOnce() -> C,58 {59 self.map_err(|error| {60 Error::from(ContextError {61 error,62 context: f(),63 })64 })59 self.map_err(|error| error.context(context()))60 }61}6268struct ContextError<E, C> {69 error: E,70 context: C,63pub(crate) struct ContextError<E, C> {64 pub error: E,65 pub context: C,66}6768impl<E, C> Debug for ContextError<E, C>69where
43 unmodified lines
modified:src/error.rs+12 added
1use crate::context::ContextError;2use std::any::TypeId;3use std::backtrace::{Backtrace, BacktraceStatus};4use std::error::Error as StdError;5use std::fmt::{self, Debug, Display};
60 unmodified lines
66 }67 }68 }6970 /// Wrap the error value with additional context.71 pub fn context<C>(self, context: C) -> Self72 where73 C: Display + Send + Sync + 'static,74 {75 Error::from(ContextError {76 error: self,77 context,78 })79 }8081 /// View this error object as the underlying error.82 pub fn as_dyn_error(&self) -> &(dyn StdError + Send + Sync + 'static) {83 &**self84 }
269 unmodified lines