rust-lang / rfcs

RFCs for changes to Rust
https://rust-lang.github.io/rfcs/
Apache License 2.0
5.96k stars 1.57k forks source link

Add details for vague std error messages #3552

Closed kukuro-kt closed 10 months ago

kukuro-kt commented 10 months ago

Current std errors are very vague. For example:

for f in vec!["a.txt","b.txt"]{
    let f=std::fs::File::open(f)?;
}

If any one file of them does not exist, we will only get this message:

The system cannot find the file specified. (os error 2)

This message does not contain filepath and have no help for debug. We have to use .map_err(...) or something else to convert it to our own error like this:

cannot open file "\xxx\xxxx\.a.txt" with err:
The system cannot find the file specified. (os error 2)

Also,

i8::try_from(12345678)?;

will only give a vague error:

out of range integral type conversion attempted

instead of

i32 12345678 is out of range i8 type conversion attempted

Almost all std errors do not provide any details about parameter and cannot be used directly, we have to handle these errors like go language in an ugly way.

In addition, some std errors have the first letter capitalized, some errors do not. Some std errors have a period at the end and some do not. This is confusing too.

kukuro-kt commented 10 months ago

As a comparison, C#'s error messages are much more useful:

System.IO.FileNotFoundException: Could not find file '\...\...\a.txt'.
File name: '\...\...\a.txt'
   at...

@shepmaster Could you please share or explain your objections? Or Integer 32, LLC does not answer anything unless paid?

nagisa commented 10 months ago

There are dozens of issues, questions and discussions about this exact issue or issues adjacent to this.

https://github.com/rust-lang/rfcs/pull/3084 is an example of one such in this repository. Although that one only covers filesystem errors, the arguments for or against making this change for other error types are the same. Closing as a duplicate.