#error-message #file-io #fs #file-path #system #operations

fs-err

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

17 stable releases (3 major)

3.0.0 Oct 23, 2024
2.11.0 Nov 20, 2023
2.9.0 Oct 31, 2022
2.7.0 Feb 19, 2022
0.1.2 Feb 10, 2020

#9 in Filesystem

Download history 237720/week @ 2024-07-29 246147/week @ 2024-08-05 255765/week @ 2024-08-12 244477/week @ 2024-08-19 263406/week @ 2024-08-26 258991/week @ 2024-09-02 258259/week @ 2024-09-09 249165/week @ 2024-09-16 282545/week @ 2024-09-23 282156/week @ 2024-09-30 277360/week @ 2024-10-07 290384/week @ 2024-10-14 288573/week @ 2024-10-21 271295/week @ 2024-10-28 284350/week @ 2024-11-04 310501/week @ 2024-11-11

1,174,431 downloads per month
Used in 941 crates (164 directly)

MIT/Apache

81KB
1.5K SLoC

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`: 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);

Feature flags

  • expose_original_error: when enabled, the Error::source() method of errors returned by this crate return the original io::Error. To avoid duplication in error messages, this also suppresses printing its message in their Display implementation, so make sure that you are printing the full error chain.

Minimum Supported Rust Version

The oldest rust version this crate is tested on is 1.40.

This crate will generally be conservative with rust version updates. It uses the autocfg crate to allow wrapping new APIs without incrementing the MSRV.

If the tokio feature is enabled, this crate will inherit the MSRV of the selected tokio version.

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

~0–5.5MB
~19K SLoC