#fs #err #error

fs-err

A drop-in replacement for std::fs with more helpful error messages

14 releases (stable)

2.7.0 Feb 19, 2022
2.6.0 May 6, 2021
2.5.0 Sep 27, 2020
2.3.0 Mar 17, 2020
0.1.2 Feb 10, 2020

#25 in Filesystem

Download history 34039/week @ 2022-03-14 34098/week @ 2022-03-21 36793/week @ 2022-03-28 34536/week @ 2022-04-04 34932/week @ 2022-04-11 35145/week @ 2022-04-18 35403/week @ 2022-04-25 33799/week @ 2022-05-02 36609/week @ 2022-05-09 36234/week @ 2022-05-16 35695/week @ 2022-05-23 36941/week @ 2022-05-30 41374/week @ 2022-06-06 46914/week @ 2022-06-13 39840/week @ 2022-06-20 39704/week @ 2022-06-27

171,585 downloads per month
Used in 104 crates (49 directly)

MIT/Apache

46KB
787 lines

fs-err

Crates.io GitHub Actions

fs-err is a drop-in replacement for std::fs that provides more helpful messages on errors. Extra information includes which operations was attempted and any involved paths.

Error Messages

Using std::fs, if this code fails:

let file = File::open("does not exist.txt")?;

The error message that Rust gives you isn't very useful:

The system cannot find the file specified. (os error 2)

...but if we use fs-err instead, our error contains more actionable information:

failed to open file `does not exist.txt`
    caused by: The system cannot find the file specified. (os error 2)

Usage

fs-err's API is the same as std::fs, so migrating code to use it is easy.

// use std::fs;
use fs_err as fs;

let contents = fs::read_to_string("foo.txt")?;

println!("Read foo.txt: {}", contents);

fs-err uses std::io::Error for all errors. This helps fs-err compose well with traits from the standard library like std::io::Read and crates that use them like serde_json:

use fs_err::File;

let file = File::open("my-config.json")?;

// If an I/O error occurs inside serde_json, the error will include a file path
// as well as what operation was being performed.
let decoded: Vec<String> = serde_json::from_reader(file)?;

println!("Program config: {:?}", decoded);

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.

No runtime deps