11# tracing-error
22
3- Utilities for instrumenting errors with [ ` tracing ` ] .
3+ Utilities for enriching error handling with [ ` tracing ` ] diagnostic
4+ information.
45
56[ ![ Crates.io] [ crates-badge ]] [ crates-url ]
67[ ![ Documentation] [ docs-badge ]] [ docs-url ]
@@ -26,7 +27,7 @@ Utilities for instrumenting errors with [`tracing`].
2627[ discord-url ] : https://discord.gg/EeF3cQw
2728[ maint-badge ] : https://img.shields.io/badge/maintenance-experimental-blue.svg
2829
29- ## Overview
30+ # Overview
3031
3132[ ` tracing ` ] is a framework for instrumenting Rust programs to collect
3233scoped, structured, and async-aware diagnostics. This crate provides
@@ -47,14 +48,9 @@ The crate provides the following:
4748
4849## Usage
4950
50- Currently, ` tracing-error ` provides the [ ` SpanTrace ` ] type, which captures
51- the current ` tracing ` span context when it is constructed and allows it to
52- be displayed at a later time.
53-
54- This crate does not _ currently_ provide any actual error types implementing
55- ` std::error::Error ` . Instead, user-constructed errors or libraries
56- implementing error types may capture a [ ` SpanTrace ` ] and include it as part
57- of their error types.
51+ ` tracing-error ` provides the [ ` SpanTrace ` ] type, which captures the current
52+ ` tracing ` span context when it is constructed and allows it to be displayed
53+ at a later time.
5854
5955For example:
6056
@@ -71,8 +67,11 @@ pub struct MyError {
7167impl fmt :: Display for MyError {
7268 fn fmt (& self , f : & mut fmt :: Formatter <'_ >) -> fmt :: Result {
7369 // ... format other parts of the error ...
70+
7471 self . context. fmt (f )? ;
72+
7573 // ... format other error context information, cause chain, etc ...
74+ # Ok (())
7675 }
7776}
7877
@@ -88,8 +87,68 @@ impl MyError {
8887}
8988```
9089
91- In the future, this crate may also provide its own ` Error ` types as well,
92- for users who do not wish to use other error-handling libraries.
90+ This crate also provides [ ` TracedError ` ] , for attaching a [ ` SpanTrace ` ] to an
91+ existing error. The easiest way to wrap errors in ` TracedError ` is to either
92+ use the [ ` InstrumentResult ` ] and [ ` InstrumentError ` ] traits or the ` From ` /` Into `
93+ traits.
94+
95+ ``` rust
96+ use tracing_error :: prelude :: * ;
97+
98+ std :: fs :: read_to_string (" myfile.txt" ). in_current_span ()? ;
99+ ```
100+
101+ Once an error has been wrapped with with a [ ` TracedError ` ] , the [ ` SpanTrace ` ]
102+ can be extracted one of three ways: either via [ ` TracedError ` ] 's
103+ ` Display ` /` Debug ` implementations, or via the [ ` ExtractSpanTrace ` ] trait.
104+
105+ For example, here is how one might print the errors but specialize the
106+ printing when the error is a placeholder for a wrapping [ ` SpanTrace ` ] :
107+
108+ ``` rust
109+ use std :: error :: Error ;
110+ use tracing_error :: ExtractSpanTrace as _;
111+
112+ fn print_extracted_spantraces (error : & (dyn Error + 'static )) {
113+ let mut error = Some (error );
114+ let mut ind = 0 ;
115+
116+ eprintln! (" Error:" );
117+
118+ while let Some (err ) = error {
119+ if let Some (spantrace ) = err . span_trace () {
120+ eprintln! (" found a spantrace:\ n {}" , spantrace );
121+ } else {
122+ eprintln! (" {:>4}: {}" , ind , err );
123+ }
124+
125+ error = err . source ();
126+ ind += 1 ;
127+ }
128+ }
129+
130+ ```
131+
132+ Whereas here, we can still display the content of the ` SpanTraces ` without
133+ any special casing by simply printing all errors in our error chain.
134+
135+ ``` rust
136+ use std :: error :: Error ;
137+
138+ fn print_naive_spantraces (error : & (dyn Error + 'static )) {
139+ let mut error = Some (error );
140+ let mut ind = 0 ;
141+
142+ eprintln! (" Error:" );
143+
144+ while let Some (err ) = error {
145+ eprintln! (" {:>4}: {}" , ind , err );
146+ error = err . source ();
147+ ind += 1 ;
148+ }
149+ }
150+ ```
151+
93152Applications that wish to use ` tracing-error ` -enabled errors should
94153construct an [ ` ErrorLayer ` ] and add it to their [ ` Subscriber ` ] in order to
95154enable capturing [ ` SpanTrace ` ] s. For example:
@@ -103,17 +162,20 @@ fn main() {
103162 // any number of other subscriber layers may be added before or
104163 // after the `ErrorLayer`...
105164 . with (ErrorLayer :: default ());
165+
106166 // set the subscriber as the default for the application
107167 tracing :: subscriber :: set_global_default (subscriber );
108168}
109169```
110170
111- [ `SpanTrace` ] : https://docs.rs/tracing-error/0.1.2/tracing_error/struct.SpanTrace.html
112- [ `ErrorLayer` ] : https://docs.rs/tracing-error/0.1.2/tracing_error/struct.ErrorLayer.html
113- [ span ] : https://docs.rs/tracing/latest/tracing/span/index.html
114- [ event ] : https://docs.rs/tracing/latest/tracing/struct.Event.html
115- [ subscriber layer ] : https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/trait.Layer.html
116- [ `tracing` ] : https://crates.io/tracing
171+ ## Feature Flags
172+
173+ - ` traced-error ` - Enables the [ ` TracedError ` ] type and related traits
174+ - [ ` InstrumentResult ` ] and [ ` InstrumentError ` ] extension traits, which
175+ provide an [ ` in_current_span() ` ] method for bundling errors with a
176+ [ ` SpanTrace ` ] .
177+ - [ ` ExtractSpanTrace ` ] extension trait, for extracting ` SpanTrace ` s from
178+ behind ` dyn Error ` trait objects.
117179
118180## License
119181
@@ -124,3 +186,19 @@ This project is licensed under the [MIT license](LICENSE).
124186Unless you explicitly state otherwise, any contribution intentionally submitted
125187for inclusion in Tracing by you, shall be licensed as MIT, without any additional
126188terms or conditions.
189+
190+ [ `SpanTrace` ] : https://docs.rs/tracing-error/*/tracing_error/struct.SpanTrace.html
191+ [ `ErrorLayer` ] : https://docs.rs/tracing-error/*/tracing_error/struct.ErrorLayer.html
192+ [ `TracedError` ] : https://docs.rs/tracing-error/*/tracing_error/struct.TracedError.html
193+ [ `InstrumentResult` ] : https://docs.rs/tracing-error/*/tracing_error/trait.InstrumentResult.html
194+ [ `InstrumentError` ] : https://docs.rs/tracing-error/*/tracing_error/trait.InstrumentError.html
195+ [ `ExtractSpanTrace` ] : https://docs.rs/tracing-error/*/tracing_error/trait.ExtractSpanTrace.html
196+ [ `in_current_span()` ] : https://docs.rs/tracing-error/*/tracing_error/trait.InstrumentResult.html#tymethod.in_current_span
197+ [ span ] : https://docs.rs/tracing/latest/tracing/span/index.html
198+ [ events ] : https://docs.rs/tracing/latest/tracing/struct.Event.html
199+ [ `Subscriber` ] : https://docs.rs/tracing/latest/tracing/trait.Subscriber.html
200+ [ subscriber layer ] : https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/trait.Layer.html
201+ [ `tracing` ] : https://docs.rs/tracing
202+ [ `std::error::Error` ] : https://doc.rust-lang.org/stable/std/error/trait.Error.html
203+ [ `SpanTrace` ] : https://docs.rs/tracing-error/0.1.2/tracing_error/struct.SpanTrace.html
204+ [ `ErrorLayer` ] : https://docs.rs/tracing-error/0.1.2/tracing_error/struct.ErrorLayer.html
0 commit comments