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: likeerr.to_string()orformat!("{err}")
Error will be printed in a single line:
External error, context: { k1: v2, k2: v2 } => external error, source: TestError: test error- Via
Debug: likeformat!("{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
impl Error
Sourcepub fn new(kind: ErrorKind, message: impl Into<String>) -> Self
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);Sourcepub fn with_context(self, key: &'static str, value: impl ToString) -> Self
pub fn with_context(self, key: &'static str, value: impl ToString) -> Self
Add more context in error.
Sourcepub fn with_source(self, source: impl Into<Error>) -> Self
pub fn with_source(self, source: impl Into<Error>) -> Self
Source§impl Error
Helper methods for Error.
impl Error
Helper methods for Error.
Sourcepub fn raw_os_io_error(raw: i32) -> Self
pub fn raw_os_io_error(raw: i32) -> Self
Helper for creating an ErrorKind::Io error from a raw OS error code.
Sourcepub fn io_error(source: Error) -> Self
pub fn io_error(source: Error) -> Self
Helper for creating an ErrorKind::Io error from std::io::Error.
Sourcepub fn no_space(capacity: usize, allocated: usize, required: usize) -> Self
pub fn no_space(capacity: usize, allocated: usize, required: usize) -> Self
Helper for creating a ErrorKind::NoSpace error with context.
Sourcepub fn join(source: JoinError) -> Self
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.
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§impl Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
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
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more