6 ms·
I agree go’s error handling feels a bit clunky, though I prefer the local error handling and passing up the chain (if it were a bit more ergonomic) to exception
by grey-area 8mo ago
I agree go’s error handling feels a bit clunky, though I prefer the local error handling and passing up the chain (if it were a bit more ergonomic) to exceptions, which IMO have a lot of other problems.
The main problems seem to me to be boilerplate and error types being so simplistic (interface just has a method returning a string). Boilerplate definitely seems solvable and a proper error interface too. I tend to use my own error type where I want more info (as in networking errors) but wish Go had an interface with at least error codes that everyone used and was used in the stdlib.
My rule of thumb on annotation is default to no, and add it at the top level. You’ll soon realise if you need more.
How would you fix it if given the chance?
- rednafi 8mo agoIn an HTTP server, top level means the handlers, is that so?
- grey-area 8mo agoYes I guess I do annotation in two places - initial error deep in libraries is annotated, this is passed back up to the initial handlers who log and respond and decide what to show users. Obviously that’s just a rule of thumb and doesn’t always apply. Depends if it can be handled lower (with a retry or default data for example), if it can be it won’t be passed all the way up. Generally though I haven’t personally found it useful to always annotate at every point in the call chain. So my default is not to annotate and if err return err. What I like about errors instead of exceptions is they are boring and predictable and in the call signature so I wouldn’t want to lose that.
- 9rx 8mo ago> I agree go’s error handling feels a bit clunky It should be the same handling as all other types. If it feels clunkier than any other type, you've not found a good design yet. Keep trying new ideas.
- grey-area 8mo agoWell two things to me feel clunky, first is less serious but leads to lots of verbosity: 1. if err != nil is verbose and distracting and happens a lot. I'd prefer say Ian Lance Taylor's suggestion of something like this where you're just going to return it vs standard boilerplate which has to return other stuff along with the error: // ? Returns error if non-nil, otherwise continue data, err := os.ReadFile(path) ? // Current situation data, err := os.ReadFile(path) if err != nil { return x,y,z,err } The second is a problem of culture more than anything but the stdlib is to blame: 2. The errors pkg and error interface has very basic string-based errors. This is used throughout the stdlib and of course in a lot of go code so we are forced to interact with it. It also encourages people to string match on errors to identify them etc etc. Yes you can use your own error types and error interfaces but this then creates interop problems and inevitably many pkgs you use return the error interface. I use my own error types, but still have to use error a lot due to stdlib etc. The wrapping they added and the annotation they encourage is also pretty horrible IMO, returning a bunch of concatted strings. So these are not things that end users of the language can fix. Surely we can do better than this for error handling?
- 9rx 8mo ago> if err != nil is verbose and distracting and happens a lot. if err != nil is no more or less verbose than if x > y. You may have a point that Go could do branching better in general, but that isn't about errors specifically. If there is something about errors that happening a lot then that still questions your design. Keep trying new ideas until it isn't happening a lot. > Surely we can do better than this for error handling? Surely we can do better for handling of all types? And in theory we can. In practice, it is like the story of generics in Go: Nobody smart enough to figure out a good solution wants to put in the work. Google eventually found a domain expert in generics to bring in as a contractor to come up with a design, but, even assuming Google is still willing to invest a lot of money in the new budget-tightening tech landscape, it is not clear who that person is in this case. Ian Lance Taylor, as you mention, tried quite hard — with work spanning over many years — in both in both cases to find a solution, which we should commend him for, but that type of design isn't really his primary wheelhouse.