pingora_core::prelude

Struct 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)>

Returns 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>,

Source§

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>,

Source§

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