Crates.io | except |
lib.rs | except |
version | 0.3.0 |
source | src |
created_at | 2019-09-06 05:28:01.85821 |
updated_at | 2023-06-01 10:35:27.816795 |
description | The only one `Error`. |
homepage | |
repository | https://github.com/jmjoy/except |
max_upload_size | |
id | 162706 |
size | 26,737 |
The only one Error
.
only available in nightly toolchain now.
The official error handling method of Rust is Result<T, E> where E: Error
.
But Error is too complicated, various types need to be converted, and each crate has its own set of Error.
Even worse, enum nesting will occur, such as:
enum BazError {
IO(std::io::Error),
}
enum BarError {
IO(std::io::Error),
Baz(BazError),
}
enum FooError {
IO(std::io::Error),
Bar(BarError),
}
How many times std::io::Error
occurs here?
The anyhow::Error
is good, but it is generally only used for
application.
This is just a personal opinion.
An Error actually only contains the following elements:
type
: Auto generated id, used to determine whether the Error is a certain type.sub_type
: Auto generated id, used to determine whether the Error is a certain sub type, used to supplement type.message
: String describing the Error.data
: Optional Error data.backtrace
: Error call stack.source
: Optional previous Error.For Rust, the message
, backtrace
,source
already exists in std::error::Error
.
Then I prefer to auto generate the type
, I think TypeId
is a solution.
For data
, I don't have the best idea, because it may be of any type. In order to achieve
only one Error, I chose to use Box<dyn Any>
internally to save it.
use except::ErrorBuilder;
pub struct MyErrorKind;
pub fn foo() -> except::Result<()> {
Err(ErrorBuilder::new::<MyErrorKind>().message("this is my error").build())
}
pub fn main() {
if let Err(ex) = foo() {
if ex.is::<MyErrorKind>() {
eprintln!("my error detected: {:?}", ex);
}
}
}
Apache-2.0