Struct pingora_core::Error

source ·
pub struct Error {
    pub etype: ErrorType,
    pub esource: ErrorSource,
    pub retry: RetryType,
    pub cause: Option<Box<dyn Error + Sync + Send>>,
    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 + Sync + Send>>

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

source

pub fn create( etype: ErrorType, esource: ErrorSource, context: Option<ImmutStr>, cause: Option<Box<dyn Error + Sync + Send>> ) -> Box<Error>

Simply create the error. See other functions that provide less verbose interfaces.

source

pub fn new(e: ErrorType) -> Box<Error>

Create an error with the given type

source

pub fn because<S, E>(e: ErrorType, context: S, cause: E) -> Box<Error>
where S: Into<ImmutStr>, E: Into<Box<dyn Error + Sync + Send>>,

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.

source

pub fn e_because<T, S, E>( e: ErrorType, context: S, cause: E ) -> Result<T, Box<Error>>
where S: Into<ImmutStr>, E: Into<Box<dyn Error + Sync + Send>>,

Short for Err(Self::because)

source

pub fn explain<S>(e: ErrorType, context: S) -> Box<Error>
where S: Into<ImmutStr>,

Create an error with context but no direct causing error

source

pub fn e_explain<T, S>(e: ErrorType, context: S) -> Result<T, Box<Error>>
where S: Into<ImmutStr>,

Short for Err(Self::explain)

source

pub fn new_up(e: ErrorType) -> Box<Error>

The new_{up, down, in} functions are to create new errors with source {upstream, downstream, internal}

source

pub fn new_down(e: ErrorType) -> Box<Error>

source

pub fn new_in(e: ErrorType) -> Box<Error>

source

pub fn new_str(s: &'static str) -> Box<Error>

Create a new custom error with the static string

source

pub fn err<T>(e: ErrorType) -> Result<T, Box<Error>>

source

pub fn err_up<T>(e: ErrorType) -> Result<T, Box<Error>>

source

pub fn err_down<T>(e: ErrorType) -> Result<T, Box<Error>>

source

pub fn err_in<T>(e: ErrorType) -> Result<T, Box<Error>>

source

pub fn etype(&self) -> &ErrorType

source

pub fn esource(&self) -> &ErrorSource

source

pub fn retry(&self) -> bool

source

pub fn set_retry(&mut self, retry: bool)

source

pub fn reason_str(&self) -> &str

source

pub fn source_str(&self) -> &str

source

pub fn as_up(&mut self)

The as_{up, down, in} functions are to change the current errors with source {upstream, downstream, internal}

source

pub fn as_down(&mut self)

source

pub fn as_in(&mut self)

source

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

source

pub fn into_down(self: Box<Error>) -> Box<Error>

source

pub fn into_in(self: Box<Error>) -> Box<Error>

source

pub fn into_err<T>(self: Box<Error>) -> Result<T, Box<Error>>

source

pub fn set_cause<C>(&mut self, cause: C)
where C: Into<Box<dyn Error + Sync + Send>>,

source

pub fn set_context<T>(&mut self, context: T)
where T: Into<ImmutStr>,

source

pub fn more_context<T>(self: Box<Error>, context: T) -> Box<Error>
where T: Into<ImmutStr>,

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.

source

pub fn root_etype(&self) -> &ErrorType

Return the ErrorType of the root Error

source

pub fn root_cause(&self) -> &(dyn Error + Sync + Send + 'static)

Trait Implementations§

source§

impl Debug for Error

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Display for Error

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Error for Error

1.30.0 · source§

fn source(&self) -> Option<&(dyn Error + 'static)>

The lower-level source of this error, if any. Read more
1.0.0 · source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type based access to context intended for error reports. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more