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

Conversation

ufuji1984
Copy link
Contributor

@ufuji1984 ufuji1984 commented Dec 10, 2018

This PR fixes issue #441.
( And also adds filename to any other Errors from opening a file. )

I wonder if there is more concise/right way to do this, so please let me know if there is.
Thanks in advance!

Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you very much for your contribution!

I have added an inline comment.

src/inputfile.rs Outdated
@@ -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))?;

and make it more concise by using map_err
Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thanks for the update!

@ufuji1984
Copy link
Contributor Author

Thanks a lot!!!

@sharkdp sharkdp merged commit 4ff3606 into sharkdp:master Dec 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants