Expand description
Enhanced error handling library built on top of error-stack.
bigerror provides ergonomic error handling adding out of the box functionality to error-stack
for common scenarios.
§Key Features
- Useful context attachments - Key-value pairs, field status, type information
- see
kv!,ty!,expect_field!
- see
- Pre-defined contexts:
NotFound,ParseError,Timeout, etc. no_stdsupport: Works in embedded and constrained environments
§Quick Start
use bigerror::{ThinContext, Report, expect_field, IntoContext, ResultIntoContext, NotFound};
// Define your error type
#[derive(ThinContext)]
struct MyError;
fn parse_number(input: &str) -> Result<i32, Report<MyError>> {
// Use context conversion for error handling
let num: i32 = input.parse()
.into_ctx::<MyError>()?; // `::<MyError>` can be omitted
Ok(num)
}
// Example with `expect_field` for optional values
fn get_config_value() -> Result<&'static str, Report<NotFound>> {
let config = Some("production");
expect_field!(config).into_ctx()
}§Attachments
Attach contextual information to errors using various attachment types:
use bigerror::{ThinContext, kv, ty, KeyValue};
#[derive(ThinContext)]
struct MyError;
// Key-value attachments
let error = MyError::attach_kv("user_id", 42);
// Type-value attachments
let data = vec![1, 2, 3];
let error = MyError::attach_kv(ty!(Vec<i32>), data.len());
#[derive(Debug, Clone, Eq, PartialEq)]
struct Username(String);
impl std::fmt::Display for Username {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
let username = String::from("alice");
assert_eq!(kv!(username.clone()), KeyValue("username", String::from("alice")));
let error = MyError::attach(kv!(username.clone())); // "username": "alice"
let username = Username(username);
assert_eq!(format!("{}", kv!(ty: username)), "<Username>: alice");§Feature Flags
std(default) - Standard library supportbacktrace(default) - Backtrace supporttracing- Integration with the tracing ecosystemserde- Serialization supportanyhow- Compatibility with anyhoweyre- Compatibility with eyre
§Pre-defined Error Contexts
Common error contexts are provided out of the box:
NotFound- Missing resources, failed lookupsParseError- Parsing and deserialization failuresTimeout- Operations that exceed time limitsInvalidInput- Validation and input errorsConversionError- Type conversion failures
See the context module for the complete list.
Re-exports§
pub use attachment::Expectation;pub use attachment::Field;pub use attachment::Index;pub use attachment::KeyValue;pub use attachment::Type;pub use error_stack;pub use context::*;
Modules§
- attachment
- Error attachment types and utilities for adding context to error reports.
- context
- Common error context types for different categories of errors.
Macros§
- bail
- Re-export of error-stack types and macros for convenience.
Creates a
Reportand returns it asResult. - ensure
- Re-export of error-stack types and macros for convenience.
Ensures
$condis met, otherwise return an error. - expect_
field - Converts an
Optionto aResult, using the extracted field name for error context. - kv
- Creates a
KeyValuepair for error attachments with flexible key-value syntax. - report
Deprecated - Re-export of error-stack types and macros for convenience.
Creates a
Reportfrom the given parameters. - ty
- Creates a
Typeattachment for the specified type.
Structs§
- Report
- Re-export of error-stack types and macros for convenience.
Contains a
Framestack consisting ofContexts and attachments.
Traits§
- Attach
Ext - Extension trait that adds attachment methods to error reports and results.
- Clear
Result - Trait for clearing either the success or error part of a
Result. - Context
Deprecated - Re-export of error-stack types and macros for convenience.
Defines the current context of a
Report. - Into
Context - Trait for converting error reports from one context type to another.
- Into
Report - Re-export of error-stack types and macros for convenience.
Provides unified way to convert an error-like structure to a
Report. - Option
Report - Extension trait for
Option<T>that provides methods to convertNoneinto error reports. - Report
As - Trait for converting
Result<T, E>intoResult<T, Report<C>>with automatic error wrapping. - Result
Ext - Re-export of error-stack types and macros for convenience.
Extension trait for
Resultto provide context information onReports. - Result
Into Context - Extension trait for
Result<T, Report<C>>that provides context conversion methods. - Thin
Context - A trait for zero-sized error types that provides convenient error creation methods.
Functions§
- init_
colour - Initialize error reporting with colored output.
- init_
emphasis - Initialize error reporting with emphasis only (no full color).
- init_
no_ ansi - Initialize error reporting with no ANSI formatting.
Derive Macros§
- Thin
Context - Derive macro for implementing
ThinContexttrait on zero-sized error types.