Trait error_chain_mini::ErrorKind [−][src]
pub trait ErrorKind { fn short(&self) -> &str; fn detailed(&self) -> String { ... } fn full(&self) -> String { ... } fn into_err(self) -> ChainedError<Self>
where
Self: Sized, { ... } fn into_with<C: ErrorContext, F>(self, op: F) -> ChainedError<Self>
where
F: FnOnce() -> C,
Self: Sized, { ... } }
Error kind type.
You can implement short
, which is exepected to return short hand description about error kind.
In addition, you can implement detailed description by detailed
, but it's optional.
Example
use std::io; use std::fs::File; use std::error::Error; use error_chain_mini::{ErrorKind, ResultExt}; enum MyErrorKind { IoError(io::Error), IndexEroor(usize), } impl ErrorKind for MyErrorKind { fn short(&self) -> &str { match *self { MyErrorKind::IoError(_) => "io error", MyErrorKind::IndexEroor(_) => "index error" } } } impl From<io::Error> for MyErrorKind { fn from(e: io::Error) -> Self { MyErrorKind::IoError(e) } } let file = File::open("not_existing_file").into_chained(|| "In io()"); assert!(file.is_err()); if let Err(e) = file { assert_eq!(e.description(), "io error"); if let MyErrorKind::IoError(ioerr) = e.kind() { assert_eq!(format!("{}", ioerr), "No such file or directory (os error 2)"); } else { panic!("error kind is incorrect"); } assert_eq!(e.contexts().collect::<Vec<_>>(), vec!["In io()"]) }
Instead of implement ErrorKind
, you can use derive.
In this case, if you don't write #[msg..
attribute, full path of the variant
(e.g. MyErrorKind::IndexError
) is used for the return value of short
.
Notes
If you derive ErrorKind
for type A, std::fmt::Display
is automatically implemented
for convinience.
Example
use std::io; use std::fs::File; use std::error::Error; use error_chain_mini::{ErrorKind, ResultExt}; #[derive(ErrorKind)] enum MyErrorKind { IoError(io::Error), IndexEroor(usize), } impl From<io::Error> for MyErrorKind { fn from(e: io::Error) -> Self { MyErrorKind::IoError(e) } } let file = File::open("not_existing_file").into_chained(|| "In io()"); assert!(file.is_err()); if let Err(e) = file { assert_eq!(e.description(), "MyErrorKind::IoError"); assert_eq!(format!("{}", e.kind()), "MyErrorKind::IoError"); if let MyErrorKind::IoError(ioerr) = e.kind() { assert_eq!(format!("{}", ioerr), "No such file or directory (os error 2)"); } else { panic!("error kind is incorrect"); } assert_eq!(e.contexts().collect::<Vec<_>>(), vec!["In io()"]) }
Required Methods
fn short(&self) -> &str
Short description of error type, compatible with std::error::Error::description
.
To avoid duplication of implement same message, we have 2 message type short/detailed.
Actually, "{}: {}", ErrorKind::short(), ErrorKind::detailed()"
is used for display
and you can also get full error message by full
method.
Provided Methods
fn detailed(&self) -> String
Detailed description of error type.
fn full(&self) -> String
Return full error message as String.
Notes Do not overrride this method.
Usage
use error_chain_mini::*; #[derive(ErrorKind, Eq, PartialEq, Debug)] #[msg(short = "My Error", detailed = "value: {}", value)] struct MyError { value: usize, } let err = MyError { value: 320 }; assert_eq!(format!("{}", err), err.full());
fn into_err(self) -> ChainedError<Self> where
Self: Sized,
Self: Sized,
Get ChainedError with no context.
Do not overrride this method.
Usage
#[derive(Clone, Copy, ErrorKind, Eq, PartialEq, Debug)] struct MyError; let chained = MyError{}.into_err(); assert_eq!(*chained.kind(), MyError {});
fn into_with<C: ErrorContext, F>(self, op: F) -> ChainedError<Self> where
F: FnOnce() -> C,
Self: Sized,
F: FnOnce() -> C,
Self: Sized,
Get ChainedError with a context.
Do not overrride this method.
Usage
fn my_func() { #[derive(Clone, Copy, ErrorKind, Eq, PartialEq, Debug)] struct MyError; let chained = MyError{}.into_with(|| "Error in my_func"); assert_eq!(*chained.kind(), MyError {}); assert_eq!(chained.contexts().nth(0).unwrap(), "Error in my_func"); }