fs-err
fs-err is a drop-in replacement for std::fs
that provides more
helpful messages on errors. Extra information includes which operations was
attmpted and any involved paths.
Error Messages
Using std::fs
, if this code fails:
let file = open?;
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 = read_to_string?;
println!;
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 File;
let file = open?;
// 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: = from_reader?;
println!;
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
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.