-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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())?;
There was a problem hiding this comment.
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`
There was a problem hiding this comment.
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
There was a problem hiding this 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!
Thanks a lot!!! |
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!