#fs #error #tracing #information #replacement #drop-in #error-context

fs-tracing

A drop-in replacement for std::fs which provides useful information on error

1 unstable release

0.1.0 Feb 25, 2021

#1251 in Filesystem

MIT/Apache

29KB
581 lines

fs-tracing

fs-tracing is a drop-in replacement for std::fs that provides an auxiliary information (such as paths) on error via tracing.

Usage

You need to install tracing_error::ErrorLayer for capturing the error context. For example, the following function installs ErrorLayer.

// https://docs.rs/tracing-error/0.1.2/tracing_error/index.html
pub fn install() {
    use tracing_error::ErrorLayer;
    use tracing_subscriber::prelude::*;

    let subscriber = tracing_subscriber::Registry::default().with(ErrorLayer::default());

    tracing::subscriber::set_global_default(subscriber).unwrap();
}

For more information, please visit https://docs.rs/tracing-subscriber/0.2.16/tracing_subscriber/registry/index.html.

Then, you can replace std::fs with fs_tracing in your code and you get nice error messages.

Errors

fs-tracing returns std::io::Error on errors for compatibility, although the returned error contains the context information such as the kind of the operation and the values passed as arguments.

For example, when you open a file which does not exist, the error message returned by fs-tracing prints the operation name (fs_tracing::read) and the offending path (/not_exist):

No such file or directory (os error 2)
Trace:
   0: fs_tracing::read
           with path="/not_exist"
             at src/lib.rs:652

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.5MB
~25K SLoC