Warm take: No language I know of does errors well
Not talking about Result
vs. exceptions here, like the actual way errors are represented
In OOP/semi-OOP langs they tend to just be classes which inherit from some sort of base Error
type
And some langs let you use any type as an error, which is also bad
Problem with the OOP method is that it makes creating new error types annoying
E.g., in Rust you need to make a whole struct, and add the Display
trait to get it to print a useful message
So I think an error should be more like a two-element tuple. The first should be like the error's type name; e.g., in a library called http
, you might have http::gzip::InvalidGzip
as the first item. The second would be a string description. That's it.
The first item would be for matching, so you can handle certain errors, and not others
The second would be how the error is shown to the user if it ends up turning into a panic/fatal
Like in Rust, since you have the freedom to use any type as a Result
's Err
field, some things return String
s, but most use types with the Error
trait
So if you want to actually handle an error, you need to understand what type it is, and hope that it actually lets you handle things how you wanted to, and if it's an Error
and you just want to pass it along, you need a Box<dyn ...>
which is annoying
@RadvylfPrograms I also think anything that could return an error should be able to specify either a list of possible error types, or a wildcard, with neither requiring creating a new type or using dynamic dispatch
So like, you could have a function which returns either a successful result, a https::tls::TlsHandshakeFailure
, a http::gzip::InvalidGzip
, or a net::NetworkUnreachable
, and if you use this function in your function, and just pass on any errors that the function you used could return, it'll automatically add those three to the list of possible errors your function could return
So for any function which possibly returns an error, you can see exactly which errors are possible, in a standardized way, and handle them conveniently