Error

Struct Error 

Source
pub struct Error { /* private fields */ }
Expand description

Error is the error struct returned by all foyer functions.

§Display

Error can be displayed in two ways:

  • Via Display: like err.to_string() or format!("{err}")

Error will be printed in a single line:

External error, context: { k1: v2, k2: v2 } => external error, source: TestError: test error
  • Via Debug: like format!("{err:?}")

Error will be printed in multi lines with more details and backtraces (if captured):

External error => external error

Context:
  k1: v2
  k2: v2

Source:
  TestError: test error

Backtrace:
   0: foyer_common::error::Error::new
             at ./src/error.rs:259:38
   1: foyer_common::error::tests::test_error_format
             at ./src/error.rs:481:17
   2: foyer_common::error::tests::test_error_format::{{closure}}
             at ./src/error.rs:480:27
   ...
  • For conventional struct-style Debug representation, like format!("{err:#?}"):
Error {
    kind: External,
    message: "external error",
    context: [
        (
            "k1",
            "v2",
        ),
        (
            "k2",
            "v2",
        ),
    ],
    source: Some(
        TestError(
            "test error",
        ),
    ),
    backtrace: Some(
        Backtrace [
            { fn: "foyer_common::error::Error::new", file: "./src/error.rs", line: 259 },
            { fn: "foyer_common::error::tests::test_error_format", file: "./src/error.rs", line: 481 },
            ...
        ],
    ),
}

Implementations§

Source§

impl Error

Source

pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self

Create a new error.

If the error needs to carry a source error, please use with_source method.

For example“

let io_error = std::io::Error::other("an I/O error occurred");
Error::new(ErrorKind::Io, "an external error occurred").with_source(io_error);
Source

pub fn with_context(self, key: &'static str, value: impl ToString) -> Self

Add more context in error.

Source

pub fn with_source(self, source: impl Into<Error>) -> Self

Set source for error.

§Notes

If the source has been set, we will raise a panic here.

Source

pub fn kind(&self) -> ErrorKind

Get the error kind.

Source

pub fn message(&self) -> &str

Get the error message.

Source

pub fn context(&self) -> &Vec<(&'static str, String)>

Get the error context.

Source

pub fn backtrace(&self) -> Option<&Backtrace>

Get the error backtrace.

Source

pub fn source(&self) -> Option<&Error>

Get the error source.

Source

pub fn downcast_ref<E>(&self) -> Option<&E>
where E: Error + Send + Sync + 'static,

Downcast the reference of the source error to a specific error type reference.

Source§

impl Error

Helper methods for Error.

Source

pub fn raw_os_io_error(raw: i32) -> Self

Helper for creating an ErrorKind::Io error from a raw OS error code.

Source

pub fn io_error(source: Error) -> Self

Helper for creating an ErrorKind::Io error from std::io::Error.

Source

pub fn no_space(capacity: usize, allocated: usize, required: usize) -> Self

Helper for creating a ErrorKind::NoSpace error with context.

Source

pub fn join(source: JoinError) -> Self

Helper for creating a ErrorKind::Join error with context.

Trait Implementations§

Source§

impl Clone for Error

Cloning an Error with large message and context can be expensive.

Be careful when cloning errors in performance-critical paths.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Error

Source§

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

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

impl Display for Error

Source§

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

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

impl Error for Error

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Scope for T

Source§

fn with<F, R>(self, f: F) -> R
where Self: Sized, F: FnOnce(Self) -> R,

Scoped with ownership.
Source§

fn with_ref<F, R>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Scoped with reference.
Source§

fn with_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Scoped with mutable reference.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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> Value for T
where T: Send + Sync + 'static,