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