Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report filename in error message #444

Merged
merged 2 commits into from
Dec 11, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/inputfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ impl<'a> InputFile<'a> {
match self {
InputFile::StdIn => Ok(InputFileReader::new(stdin.lock())),
InputFile::Ordinary(filename) => {
let file = File::open(filename)?;
let file = match File::open(filename) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use .map_err here. Something like:

let file = File::open(filename).map_err(format!(...).into())?;

Copy link
Contributor Author

@ufuji1984 ufuji1984 Dec 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your advice!
I tried it, but I couldn't get it built...
Seems out of my hands... 😅

62 |                 let file = File::open(filename).map_err(format!("{}: {}", filename, e).into())?;                                                    
   |                                                                                     ^ not found in this scope   
62 |                 let file = File::open(filename).map_err(|e| format!("{}: {}", filename, e).into())?;
   |                                                 ^^^^^^^ cannot infer type for `F` 
62 |                 let file = File::open(filename).map_err(|e:Error| format!("{}: {}", filename, e).into())?;  
   |                                                 ^^^^^^^ ------------------- found signature of `fn(errors::Error) -> _`
   |                                                 |                                                           
   |                                                 expected signature of `fn(std::io::Error) -> _`  
62 |                 let file = File::open(filename).map_err(|e: std::io::Error| -> std::io::Error {format!("{}: {}", filename, e).into()})?;            
   |                                                                                                                               ^^^^ the trait `std::convert::From<std::string::String>` is not implemented for `std::io::Error`

Copy link
Owner

@sharkdp sharkdp Dec 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was wrong. .into() is not needed. This should work (I'd like to add the additional quotes around the filename):

File::open(filename).map_err(|e| format!("'{}': {}", filename, e))?;

Ok(f) => f,
Err(e) => return Err(format!("{}: {}", filename, e).into()),
};

if file.metadata()?.is_dir() {
return Err(format!("'{}' is a directory.", filename).into());
Expand Down