pub struct Error {
pub etype: ErrorType,
pub esource: ErrorSource,
pub retry: RetryType,
pub cause: Option<Box<dyn ErrorTrait + 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 ErrorTrait + 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 ErrorTrait + Send + Sync>>,
) -> BError
pub fn create( etype: ErrorType, esource: ErrorSource, context: Option<ImmutStr>, cause: Option<Box<dyn ErrorTrait + Send + Sync>>, ) -> BError
Simply create the error. See other functions that provide less verbose interfaces.
Sourcepub fn because<S: Into<ImmutStr>, E: Into<Box<dyn ErrorTrait + Send + Sync>>>(
e: ErrorType,
context: S,
cause: E,
) -> BError
pub fn because<S: Into<ImmutStr>, E: Into<Box<dyn ErrorTrait + Send + Sync>>>( e: ErrorType, context: S, cause: E, ) -> BError
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: Into<ImmutStr>, E: Into<Box<dyn ErrorTrait + Send + Sync>>>(
e: ErrorType,
context: S,
cause: E,
) -> Result<T>
pub fn e_because<T, S: Into<ImmutStr>, E: Into<Box<dyn ErrorTrait + Send + Sync>>>( e: ErrorType, context: S, cause: E, ) -> Result<T>
Short for Err(Self::because)
Sourcepub fn explain<S: Into<ImmutStr>>(e: ErrorType, context: S) -> BError
pub fn explain<S: Into<ImmutStr>>(e: ErrorType, context: S) -> BError
Create an error with context but no direct causing error
Sourcepub fn e_explain<T, S: Into<ImmutStr>>(e: ErrorType, context: S) -> Result<T>
pub fn e_explain<T, S: Into<ImmutStr>>(e: ErrorType, context: S) -> Result<T>
Short for Err(Self::explain)
Sourcepub fn new_up(e: ErrorType) -> BError
pub fn new_up(e: ErrorType) -> BError
The new_{up, down, in} functions are to create new errors with source {upstream, downstream, internal}
pub fn new_down(e: ErrorType) -> BError
pub fn new_in(e: ErrorType) -> BError
pub fn err<T>(e: ErrorType) -> Result<T>
pub fn err_up<T>(e: ErrorType) -> Result<T>
pub fn err_down<T>(e: ErrorType) -> Result<T>
pub fn err_in<T>(e: ErrorType) -> Result<T>
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: BError) -> BError
pub fn into_up(self: BError) -> BError
The into_{up, down, in} are the same as as_* but takes self
and also return self
pub fn into_down(self: BError) -> BError
pub fn into_in(self: BError) -> BError
pub fn into_err<T>(self: BError) -> Result<T>
pub fn set_cause<C: Into<Box<dyn ErrorTrait + Send + Sync>>>( &mut self, cause: C, )
pub fn set_context<T: Into<ImmutStr>>(&mut self, context: T)
Sourcepub fn more_context<T: Into<ImmutStr>>(self: BError, context: T) -> BError
pub fn more_context<T: Into<ImmutStr>>(self: BError, context: T) -> BError
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