by c7DJTLrn · 4y ago · 0 comments
are we talking about the same go where the average code looks like this, a big long chain of if blabla return nil, err ?
https://github.com/cockroachdb/cockroach/blob/5fb4478b94ecaf...
like, code like this is exactly the reason why exceptions were invented
Agree that Rust errors need to be better. anyhow and thiserror are fine libraries but I want that experience out of the box, ideally. There are ongoing efforts to improve things, so I’m hopeful.
fmt.Errorf("... %w", err)
>compare errors errors.Is(err, ErrorType)For example, if I have some library that makes a network request and that request times out do I:
1. Return net.Error?
2. fmt.Errorf?
3. My own custom type
The problem with (1), is that API is invisible; the caller is now expected to trawl through my code or documentation for any errors I might return that the caller might want to handle. (2) prevents the caller for acting on my error without string mangling (and again has to inspect my code for the exact strings to handle. (3) is the best, but has the same level of boilerplate; and is also, I've found, unidiomatic in Go.
The second blight of Go's error handling situation (after if err == nil), is errors are rarely part of an APIs contract; especially outside of the standard library. Often times if you want to handle a special error code for a given API it means actually opening up the source code and hunting down where that error is thrown and hoping that (1) the error type is public or (2) that the library author won't break your string mangling in a later version.
But that's wrong. When you call String() on it, it will look like the error is just a concatenated string, but it preserves all information about the underlying error, that's the whole point of the %w directive.
I would recommend it for application code. For library code you should go the extra mile and use proper error types.
I think that Go's error handling is actually fine and most people are making a fuss about nothing, but even so Rust's error handling is clearly superior.
If you do need to know specifically what the error was then the API provides public error types.
Because it's always unergonomic?
If you're properly wrapping errors it isn't.
fmt.Errorf("... %w", err)I think you misunderstand me. I'm not complaining that there is no context. I'm complaining about destroying all structured content. If you want information out of that, you have to string parse your errors! No type checking or anything useful on error types either.
Yes, it's possible, but that usually comes with 3rd party libraries that manage errors with structure. Such as a large array of arrays producing a structured "stack". But if a single person uses string formatting with your nice structured error? Goodbye structure, hello string parsing.
It's a very, very low bar.
>If you want information out of that, you have to string parse your errors
I'm trying not to be an asshole here, but have you actually played with Go? It seems a lot of the Go hate comes from people with extremely trivial knowledge of it.
The %w directive is not just a simple string format/concat. It constructs an error which when String() is called on it, will appear as a concat'd string, but the underlying error is preserved.
I agree, I love Rust, but I'd like to see error handling ergonomics improved.
I'd like to see error handling become more like Zig or Swift.
Until then, I cope with the `anyhow` and `thiserror` crate :|