https://github.com/jmjoy/except
The only one `Error`.
https://github.com/jmjoy/except
error error-handler exception panic throw try try-catch
Last synced: 3 months ago
JSON representation
The only one `Error`.
- Host: GitHub
- URL: https://github.com/jmjoy/except
- Owner: jmjoy
- License: apache-2.0
- Created: 2019-09-15T04:39:01.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-06-01T10:35:06.000Z (almost 2 years ago)
- Last Synced: 2025-02-15T14:53:40.190Z (3 months ago)
- Topics: error, error-handler, exception, panic, throw, try, try-catch
- Language: Rust
- Homepage: https://crates.io/crates/except
- Size: 22.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Except
The only one `Error`.
**only available in nightly toolchain now.**
## Why?
The official error handling method of Rust is `Result 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:
```rust
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`](https://crates.io/crates/anyhow) is good, but it is generally only used for
application.## Solution
*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` internally to save it.## Example
```rust
use except::ErrorBuilder;pub struct MyErrorKind;
pub fn foo() -> except::Result<()> {
Err(ErrorBuilder::new::().message("this is my error").build())
}pub fn main() {
if let Err(ex) = foo() {
if ex.is::() {
eprintln!("my error detected: {:?}", ex);
}
}
}
```## License
Apache-2.0