pub struct Error {
pub etype: ErrorType,
pub esource: ErrorSource,
pub retry: RetryType,
pub cause: Option<Box<dyn Error + Send + Sync>>,
pub context: Option<ImmutStr>,
}
Expand description
The struct that represents an error
Fields§
§etype: ErrorType
the type of error
esource: ErrorSource
the source of error: from upstream, downstream or internal
retry: RetryType
if the error is retry-able
cause: Option<Box<dyn Error + Send + Sync>>
chain to the cause of this error
context: Option<ImmutStr>
an arbitrary string that explains the context when the error happens
Implementations§
Source§impl Error
impl Error
Sourcepub fn create(
etype: ErrorType,
esource: ErrorSource,
context: Option<ImmutStr>,
cause: Option<Box<dyn Error + Send + Sync>>,
) -> Box<Error>
pub fn create( etype: ErrorType, esource: ErrorSource, context: Option<ImmutStr>, cause: Option<Box<dyn Error + Send + Sync>>, ) -> Box<Error>
Simply create the error. See other functions that provide less verbose interfaces.
Sourcepub fn because<S, E>(e: ErrorType, context: S, cause: E) -> Box<Error>
pub fn because<S, E>(e: ErrorType, context: S, cause: E) -> Box<Error>
Create an error with the given type, a context string and the causing error. This method is usually used when there the error is caused by another error.
use pingora_error::{Error, ErrorType, Result};
fn b() -> Result<()> {
// ...
Ok(())
}
fn do_something() -> Result<()> {
// a()?;
b().map_err(|e| Error::because(ErrorType::InternalError, "b failed after a", e))
}
Choose carefully between simply surfacing the causing error versus Because() here. Only use Because() when there is extra context that is not capture by the causing error itself.
Sourcepub fn e_because<T, S, E>(
e: ErrorType,
context: S,
cause: E,
) -> Result<T, Box<Error>>
pub fn e_because<T, S, E>( e: ErrorType, context: S, cause: E, ) -> Result<T, Box<Error>>
Short for Err(Self::because)
Sourcepub fn explain<S>(e: ErrorType, context: S) -> Box<Error>
pub fn explain<S>(e: ErrorType, context: S) -> Box<Error>
Create an error with context but no direct causing error
Sourcepub fn e_explain<T, S>(e: ErrorType, context: S) -> Result<T, Box<Error>>
pub fn e_explain<T, S>(e: ErrorType, context: S) -> Result<T, Box<Error>>
Short for Err(Self::explain)
Sourcepub fn new_up(e: ErrorType) -> Box<Error>
pub fn new_up(e: ErrorType) -> Box<Error>
The new_{up, down, in} functions are to create new errors with source {upstream, downstream, internal}
pub fn new_down(e: ErrorType) -> Box<Error>
pub fn new_in(e: ErrorType) -> Box<Error>
pub fn err<T>(e: ErrorType) -> Result<T, Box<Error>>
pub fn err_up<T>(e: ErrorType) -> Result<T, Box<Error>>
pub fn err_down<T>(e: ErrorType) -> Result<T, Box<Error>>
pub fn err_in<T>(e: ErrorType) -> Result<T, Box<Error>>
pub fn etype(&self) -> &ErrorType
pub fn esource(&self) -> &ErrorSource
pub fn retry(&self) -> bool
pub fn set_retry(&mut self, retry: bool)
pub fn reason_str(&self) -> &str
pub fn source_str(&self) -> &str
Sourcepub fn as_up(&mut self)
pub fn as_up(&mut self)
The as_{up, down, in} functions are to change the current errors with source {upstream, downstream, internal}
pub fn as_down(&mut self)
pub fn as_in(&mut self)
Sourcepub fn into_up(self: Box<Error>) -> Box<Error>
pub fn into_up(self: Box<Error>) -> Box<Error>
The into_{up, down, in} are the same as as_* but takes self
and also return self
pub fn into_down(self: Box<Error>) -> Box<Error>
pub fn into_in(self: Box<Error>) -> Box<Error>
pub fn into_err<T>(self: Box<Error>) -> Result<T, Box<Error>>
pub fn set_cause<C>(&mut self, cause: C)
pub fn set_context<T>(&mut self, context: T)
Sourcepub fn more_context<T>(self: Box<Error>, context: T) -> Box<Error>
pub fn more_context<T>(self: Box<Error>, context: T) -> Box<Error>
Create a new error from self, with the same type and source and put self as the cause
use pingora_error::Result;
fn b() -> Result<()> {
// ...
Ok(())
}
fn do_something() -> Result<()> {
// a()?;
b().map_err(|e| e.more_context("b failed after a"))
}
This function is less verbose than Because
. But it only work for Error while
Because
works for all types of errors who implement std::error::Error trait.
Sourcepub fn root_etype(&self) -> &ErrorType
pub fn root_etype(&self) -> &ErrorType
Return the ErrorType of the root Error
pub fn root_cause(&self) -> &(dyn Error + Send + Sync + 'static)
Trait Implementations§
Source§impl Error for Error
impl Error for Error
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Auto Trait Implementations§
impl Freeze for Error
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string
, but without panic on OOM.