Crates.io | smarterr |
lib.rs | smarterr |
version | 1.0.6 |
source | src |
created_at | 2022-12-22 19:40:50.698498 |
updated_at | 2024-09-01 20:39:59.879746 |
description | Smart error handling library |
homepage | |
repository | https://github.com/mozalmic/smarterr |
max_upload_size | |
id | 744081 |
size | 24,034 |
SmartErr, an error handling library, introduces several convenient aproaches to define, raise and handle domain-specific errors in libraries and/or applications.
With SmartErr it is possible to:
raise
and throw
methods on regular types (numbers, strings, boolean, Option, Result, etc) as an error source. Look into Raising errors section to find out more details.error!
macro (requires atomic_error
feature).erorrset
feature).With atomic_error
feature enabled (it is also included into smarterr
feature), it is possible to define atomic errors, which are structs, annotated with thiserror::Error
attribute. For instance, the following definition:
error!(pub InvalidData<ParseIntError> { pub data: String } => "Invalid data provided [data={data}]");
will generate the code:
#[derive(thiserror::Error, Debug)]
#[error("Invalid data provided [data={data}]")]
pub struct InvalidData {
pub data: String,
#[source]
pub source: ParseIntError,
}
Sometimes functions may return simple types instead of Result. Library provides a set of methods to convert these types into Result based on the convention what values should be treated as an error:
Source type | error state for the type |
---|---|
numbers (i32, usize, etc) | != 0 |
bool | false |
strings (&str, String etc) | is_empty() |
Option | None |
Result | Err |
Further actions depend on the function used to convert the value into Result:
throw - does NOT change error context. When the error state is detected, the value is mapped with the provided err_map
function and returned as Err(err_map(original_value))
. Otherwise, the original value is just wrapped into Result::Ok.
raise - CHANGES error context. When the error state is detected, the original value is wrapped into Result::Ok. Otherwise, the value is mapped with the provided ok_to_err_map
function and returned as Err(ok_to_err_map(original_value))
.
With errorset
feature enabled, it is possible to define a set of errors emitted by the function. The errorset
macro generates a new error type that contains all the errors from the function signature.
You may find more details in the ErrorSet crate documentation.
SmartErr
crate reexports ErrorSet
crate, so you may use it functionality directly.
For instance, the following code:
#[errorset]
pub fn parse_u32(data: &str) -> Result<u32, (InvalidData, OutOfBounds)> {
...
}
will produce the code:
#[derive(::thiserror::Error, Debug)]
pub enum ParseU32Errors {
#[error(transparent)]
InvalidData(#[from] InvalidData),
#[error(transparent)]
OutOfBounds(#[from] OutOfBounds),
}
impl _SomeStruct {
fn parse_u32(data: &str) -> Result<u32, ParseU32Errors> {
...
}
}