6 ms·
Error handling in Go web apps shouldn't be so awkward
- Woshiwuja 3y agoWe just need an option type, thats it
- kcartlidge 3y agoThere's so much contention over error handling in Go. Over the years I've settled on treating it like gofmt: I may not 100% agree with all the choices, but it is what it is so let's just get on with it. And to be honest as a majority-C# dev I quite like the break from exception handling. As an aside, one thing I've found which makes life easier is to invert the check. With convoluted logic it can lead to excessive nesting, but then nobody would write code that did enough in a single function to make that an issue, surely? Practical example. Here's what you might normally see: err := doFunctionA() if err != nil { handleErrorFromFunctionA() } err = doFunctionB() if err != nil { handleErrorFromFunctionB() } err = doFunctionC() if err != nil { handleErrorFromFunctionC() } I'd usually just switch it: err := doFunctionA() if err == nil { err = doFunctionB() if err == nil { err = doFunctionC() } } if err != nil { handleErrorRegardlessOfSource() } Of course it only works if the errors can be sensibly handled the same way regardless of their source, but that's surprisingly often the case (eg log, display, and/or return it). It's just a minor change but it's standard uncomplicated Go, and with multiple sequential operations it reduces the code-waffle even more the larger the code gets. YMMV and it isn't always suitable, but every little helps.
- Fin_Code 3y agoThe whole point of error handling in go is that magic does not happen. Large error handling frameworks can create tracing difficulties. In the authors example he will never be confused as to where the error occurred. Is it overly verbose, yes. Do I prefer it yes. I am quickly able to identify and address issues.
- kitkat_new 3y agowell, no magic, goal reached. However, the current implementation of error handling is atrocious. If Go would take sum types and question marks from Rust, Go would keep the general error handling strategy and philosophy with the difference of being way less verbose and more ergonomic.
- nonethewiser 3y ago> If Go would take sum types and question mark Go is an incomplete language masquerading as a simple one.
- riwsky 3y agoFrom “the rise of worse is better”: > Completeness -- the design must cover as many important situations as is practical. All reasonably expected cases should be covered. Completeness can be sacrificed in favor of any other quality. In fact, completeness must be sacrificed whenever implementation simplicity is jeopardized. Consistency can be sacrificed to achieve completeness if simplicity is retained; especially worthless is consistency of interface.
- MobiusHorizons 3y agoI get that you don’t like some of the features go has done without, but how do you justify incomplete? What do you even mean by that word? Clearly lots of code has been written in it. Its deficiencies do not prevent programs from being expressed. The only definition I can think of for complete would be Turing completeness, but that is clearly too low a bar for what you are saying.
- bananapub 3y ago> but how do you justify incomplete are you seriously suggesting that Go's insistence of requiring the programmer to scatter "if err != nil {}" after most function calls - with no compiler assistance if you forget - is complete either in the sense of "having all necessary parts, elements, or steps" or "being finished"? it's terrible, causes bugs and it seems incredibly unlikely to me that it won't be improved.
- calebio 3y agoI like the way this pattern trends, but one downside is that it mixes HTTP concerns into the service layer. E.g. the service layer needs to know exactly which HTTP error code this responds to when returning an error. If the service layer is used for something other than a web server, e.g. a handler for a queue, we've now got a kinda funky abstraction. I notice that the HTTP Request and ResponseWriter is also mixed into the service layer, which is another thing I would probably move one step closer to the actual HTTP server and away from the logic that interacts with the database. In the past I've used `Temporary()` or `Permanent()` along with some other additional fields to help map to HTTP status codes at the HTTP server's handler layer.
- flimzy 3y agoPost author here. For any non-trivial app, there would ideally be a set of domain error types/codes that the various application layers know about, rather than concerning themselves with HTTP statuses. I didn't discuss that in the article, because I felt like it was a bit tangential. Perhaps it was a mistake to omit that detail.
- cpuguy83 3y agoI've done interfaces where centered around the kind of error that happened, which is typically standard across apps and translates well into status codes for http or grpc. Eg "NotFound()" "InvalidParameter()" "Unknown()". That said these could all just be sentinel errors with the normal unwrapping in the stdlib these days.
- lIIllIIllIIllII 3y agoThis is def an anti-pattern. As soon as you have your services suddenly hooked up to something else - chron (or hangfire or whatever) jobs or a message bus - it gets a bit weird because your service layer thinks you're still responding to an HTTP request and whoever works on the service now has to think about HTTP as well which is pretty fucking bizarre to begin with. And your unit tests for a business logic service method are gonna look at HTTP errors? It feels dirty, but not in a fun tap-your-nose way. It's really not hard to just return errors relating to what actually happened (can't find user by id, user is blocked from logging in, whatever) and have the app's ingress points (API controllers, message handlers, job runners) decide how to surface that in a way that makes sense for the specific interface.
- mappu 3y agoIn this example, you have to magically know that DB returns a statusError{} in order not to wrap it again. I've found one improvement is to encode this knowledge in the type system, by not using plain error returns on the handler, but instead use something like e.g. type netError interface { Error() string Public() bool StatusCode() int Unwrap() error } func PublicError(e error, code int) netError { ... } func InternalError(e error) netError { ... } func SomeHandler(h *http.Request, w http.ReponseWriter) netError { ... } This makes it clearer whether the underlying err "knows" if it should be public or private.
- earthboundkid 3y agoThis seems like a good idea, but it will break lots of things. In particular, err != nil will break. Don’t do it.
- mappu 3y agoerr != nil works fine, exactly the same way it works for plain error: interface values can be nil,
- earthboundkid 3y agoWith this err will never equal nil.
- deleted 3y ago[deleted]
- binary132 3y agoReturning interfaces is an antipattern masquerading as a design pattern... Just saying
- xargon7 3y agohttps://github.com/augustoroman/sandwich https://github.com/augustoroman/sandwich is another approach for achieving a similar result. One nice aspect of writing handlers that return errors is that they are easy to test and clean to write. Sandwich does very simple sequencing to achieve an understandable dependency injection mechanism that produces clear errors when something goes wrong, and it produces errors at setup time rather than run time.
- latchkey 3y agoInteresting. The DI can also be done with something like UberFX.
- neonsunset 3y ago[flagged]
- deleted 3y ago[deleted]
- Xeoncross 3y agoIn general, the way you output errors to the client and the errors you log are two totally different things. This is a slight tweak from the problem the author is attempting to solve by letting the client see the error. Like the author, I created a special error interface I can look for when returning client errors: https://github.com/Xeoncross/public-error-go https://github.com/Xeoncross/public-error-go
- deleted 3y ago[deleted]
- mholt 3y agoThe article basically describes how Caddy does error handling in HTTP routes. https://github.com/caddyserver/caddy/blob/cb86319bd50322d4ac9e730b2fc5639daa24b82a/modules/caddyhttp/errors.go#L57 https://github.com/caddyserver/caddy/blob/cb86319bd50322d4ac...
- earthboundkid 3y agoYeah and my blog post from 2020. It’s a convergent solution.
- segfaltnh 3y agoDid anyone else immediate get confused by the assertion that the http code shouldn't know about the database details so the solution is for the database code to embed http details instead? In our project we have a domain types package with errors that permeate these types of boundaries. It's "okay" in the ways everything in Go is "okay". I've yet to see anything I really like in this space.
- flimzy 3y agoPost author here. I probably could have made this a bit more clear, but I feel it was a slight tangent to the main point of the post: In any serious app, I wouldn't actually set an HTTP status directly in the db layer. Rather, I would do what others in this thread have suggested: I'd have a set of domain error types/codes, and my DB layer would set _that_. That domain error value would then implement the `HTTPStatus()` method to return the appropriate status code. This allows you to have multiple "not found" error codes (as an example) for internal use, which all resolve to an HTTP status of 404.
- earthboundkid 3y agoYeah, you should have an error domain and convert a sql.NotFound error to an app.NotFound error at the boundary. The http handler can then treat those as 404s.
- deleted 3y ago[deleted]
- randomdata 3y agoYes. In fact, I am continually confused by how as soon as developers see the word "error" they forget how to write software. They can be perfectly competent developers when handling any other type, but as soon as "error" shows up on the screen all bets are off. It is bizarre. Developers as a rule also seem to act strange if you suggest that they made an error. Perhaps the word "error" is triggering in some deep-seated emotional way? I wonder if, in software, we called errors "pineapples" instead if it would avoid triggering such emotions, allowing one to reason about errors in the same rational way they do all other values?
- jrockway 3y agoI agree with this. I usually wrap my HTTP handlers in a function that copies the error to the HTTP response, and can then just return an error from my actual handler. I think it's fair to not force this in the standard library. HTTP doesn't have the concept of errors; just a status code, headers, and body (ok ok, and 93 other fun things you can do; chunked responses, upgrades, etc.). If you want to write the wrapper, write the wrapper. If you don't, don't.
- stlava 3y agoSounds like the author is missing an interface to abstract the DB backend.
- wruza 3y agoDistilled, it’s the wet dream of mapping errors/exceptions to http codes. I just decided that 200 ok, 400 you, 500 me is enough for non-file and/or non-browser-itself-requested-it requests. And I like much more when API docs don’t require me to set switch-cases around their return codes, because it all ends up in a single “if not 200, log status and body and throw/return '{you|me} failed'”. Moreover, I’ve seen things returning e.g. an array of >0 orders with 200 and an empty array with 404. This makes me emotional and breaks HN guidelines.
- emmanueloga_ 3y agoJust a tiny bit of sugar would go a long way silencing 50% of the complains about error handling in go... Not an error handling improvement per-se, but if we had to sprinkle just one line of code instead of three around all spots of error handling, that would make me so much happier :-) From: if err != nil { return err } To: return err if err != nil ... that is, allow ruby-style placing of conditionals after the return.
- akira2501 3y agoIn these cases I'll almost always name the output error parameter. func x() (err error) The short variable declaration syntax only requires that _at least one_ name is unique, so you can freely use the variable even in := sequences. So you end up with something like: if thing, err := impl(); err != nil { return } else { // use thing } In the articles first example, the "problem" is that you have a core of a method that can return error or a widgetJSON. There should be a separate function that does precisely that. If you want to mash your http logic and your use logic into the same function, you're going to have a mess no matter how hard you try.
- emmanueloga_ 3y agoWhat I'm suggesting would make this snippet nicer too: return err if thing, err := impl(); err != nil // use thing It would remove the indent, and plus you wouldn't need to name your return variable, that can sometimes be error prone (say, if you end up returning the wrong thing). Just a lil bit of sugar!
- calebio 3y agoI might be in the minority here, but IMO that snippet does not look nicer and looks pretty ugly. You've got like 3 things going on all jumbled together. I would not want to encounter that line of code in the wild, or 3 months after writing it, and attempt to parse what it's doing.
- randomdata 3y ago
- marcus_holmes 3y agoI've started playing around with using the ResponseRecorder from httptest to create a middleware that records the response from a handler and then deals with it appropriately: - if it's an error then display an actual error page (or redirect to login, or whatever) - if it's 200 OK then minimise the logging to just "successfully processed request for GET /foo?bar" I like OP's way of dealing with the "http.Error then return" problem, especially since I usually find I'm also logging the error (so slog.Error, then http.Error, then return, which is painful since I'm repeating myself for the Error calls). But I don't like messing with the http.Handler interface. I think there's an approach involving both - like creating a type that implements http.Handler and handles logging, error reporting and error redirection while still allowing the core function to just "return err". I'm going to experiment with this. Thanks for the thought OP :)