You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// A main function that returns AnyError (via the crate's Result alias)
55
-
fnrun() ->Result<()> {
56
-
// We can add context to errors via the result extensions.
57
-
// The `context` function adds context to any `StackError`.
58
-
some_fn(13).context("failed at some_fn")?;
59
-
// To add context to std errors, we have to use `std_context` from `StdResultExt`.
60
-
fail_io().std_context("failed at fail_io")?;
57
+
/// The `Result` type defaults to `AnyError` for the error variant.
58
+
///
59
+
/// Errors types using the derive macro convert to `AnyError`, and we can add additional context
60
+
/// with the result extensions.
61
+
fnprocess(input:u32) ->Result<()> {
62
+
validate_input(input).context("failed to process input")?;
61
63
Ok(())
62
64
}
65
+
```
63
66
64
-
fnmain() ->Result<()> {
65
-
leterr=run().unwrap_err();
66
-
assert_eq!(
67
-
format!("{err:#}"),
68
-
"failed at some_fn: bad input (13)"
69
-
);
70
-
Ok(())
71
-
}
67
+
The error returned from `process` would look like this in the alternate display format:
68
+
```text
69
+
failed to process input: invalid input: wanted 23 but got 13
70
+
```
71
+
and like this in the debug format with `RUST_BACKTRACE=1` or `RUST_ERROR_LOCATION=1`:
72
+
```text
73
+
failed to process input (examples/basic.rs:61:17)
74
+
Caused by:
75
+
invalid input (examples/basic.rs:36:5)
76
+
wanted 23 but got 13 (examples/basic.rs:48:13)
77
+
```
72
78
73
-
/// You can also use the macros with tuple structs or enums.
74
-
/// In this case the meta field will be added as the last field.
75
-
#[stack_error(derive, add_meta)]
76
-
#[error("tuple fail ({_0})")]
77
-
structTupleStruct(u32);
79
+
### Details
78
80
79
-
#[stack_error(derive, add_meta)]
80
-
enumTupleEnum {
81
-
#[error("io failed")]
82
-
Io(#[error(source, std_err)] std::io::Error),
83
-
}
81
+
- All errors using the macro implement the `StackError` trait, which exposes call-site metadata for
82
+
where the error occurred. Its `source` method returns references which may be other stack errors,
83
+
allowing access to location data for the entire error chain.
84
+
- The proc macro can add a `meta` field to structs or enum variants. This field stores call-site
85
+
metadata accessed through the `StackError` trait.
86
+
* Call-site metadata in the `meta` field is collected only if `RUST_BACKTRACE=1` or
87
+
`RUST_ERROR_LOCATION=1` env variable is set. Otherwise, it is disabled to avoid runtime overhead.
88
+
* The declarative macro `e!` provides an ergonomic way to construct such errors without
89
+
explicitly setting the field. The crate's `ensure` and `bail` macro also do this.
90
+
- The crate includes an `AnyError` type, similar to `anyhow::Error`. When created from a
91
+
`StackError`, the call-site metadata is preserved. `AnyError` is recommended for applications and
92
+
tests, while libraries should use concrete derived errors.
93
+
* All stack errors convert to `AnyError`, so they can be propagated to such results with `?`.
94
+
* For std errors, use `std_context` or `anyerr` to convert to `AnyError`. For stack errors, use
95
+
`context` or simply propagate with `?`.
96
+
* Result extension traits provide conversions between results with `StackError`s,
97
+
`std::error::Error`s to `AnyError`, with support for attaching context.
98
+
- Both `AnyError` and all errors using the `StackError` derive feature consistent, structured output
99
+
that includes location metadata when available.
84
100
85
-
```
101
+
### Feature flags
102
+
103
+
*`anyhow` (off by default): Enables `From<anyhow::Error> for AnyError` and `From<AnyError> for anyhow::Error`
104
+
105
+
## License
106
+
107
+
Copyright 2025 N0, INC.
108
+
109
+
This project is licensed under either of
110
+
111
+
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
112
+
http://www.apache.org/licenses/LICENSE-2.0)
113
+
* MIT license ([LICENSE-MIT](LICENSE-MIT) or
114
+
http://opensource.org/licenses/MIT)
115
+
116
+
at your option.
117
+
118
+
## Contribution
119
+
120
+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
0 commit comments