commit | 9df3aaa070ad2705e9dbdd5233bd652646b81d57 | [log] [tgz] |
---|---|---|
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | Sun May 26 07:09:01 2024 +0000 |
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | Sun May 26 07:09:01 2024 +0000 |
tree | e61aaa4ad860616ecff7c8aee14b0b447f8c0e97 | |
parent | b7bf09024037e6755e67f44c2770e71dd199917b [diff] | |
parent | 39c07d1d77a0205d0566f15cf4375d3ace875376 [diff] |
Snap for 11878398 from 39c07d1d77a0205d0566f15cf4375d3ace875376 to busytown-mac-infra-release Change-Id: I869e0aba3fa41cfceca20caa12d07b5a96e1367b
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.
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)
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);
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.
Licensed under either of
at your option.
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.