Gopher Wrangling: Effective error handling in Go
stephenn.com
Gopher Wrangling: Effective error handling in Go
1–10 of 310 posts
Re: Gopher Wrangling: Effective error handling in Go
#2Re: Gopher Wrangling: Effective error handling in Go
#3For #4 wrapping your errors creates pretty and logical error messages for free. It should be done in most cases.
Re: Gopher Wrangling: Effective error handling in Go
#4I added .Empty() and .Partial() because if you're returning "string, error" from a function, for example, then "" doesn't cut it for me and instead of checking for "" in the calling function, I can instead check for err.Empty(). This doesn't seem like it's useful, but take that idea and apply it to two additional scenarios: a non-pointer to a struct{} with 10 fields (are they all empty?), and partial return values i.e. the function you called threw a warning and only partially populated the return value. Now the calling function can shift the "is empty" checks to the function that actually constructs the return value (or not.)
Now I can call a function, get my custom error type back, and I can determine if there was an issue and whether or not the value is empty or partial regardless of the type (and its complexity.) This paid me back in dividends the moment I wanted to be able to return a warning and a partial result - so not workflow breaking, but also not everything the caller asked for... it's up to the caller to determine if it has what it needs to continue.
Re: Gopher Wrangling: Effective error handling in Go
#5I understand that maybe the language authors in the early days didn’t want to lock anyone into a strict paradigm for how to deal with errors. Like I’m not thrilled about Java’s approach either, but that can never change. But Go is a very popular and established language now. It’s time to fix the error handling mess. There are so many good examples out there to get inspiration from. F#, Swift and Rust have a perfect error handling mechanism.
Re: Gopher Wrangling: Effective error handling in Go
#6Re: Gopher Wrangling: Effective error handling in Go
#7Re: Gopher Wrangling: Effective error handling in Go
#8Always wrapping errors can be a good way to get a stack trace of the error path in the logs.
Re: Gopher Wrangling: Effective error handling in Go
#9I've tried to like go's verbose error handling (follow the “happy path”) but the error handling signal to noise ratio is skewed in a way that makes developing in go feel slow and boring.
Re: Gopher Wrangling: Effective error handling in Go
#10There is a lot that I like about Go. Error handling is not one of them. On one hand, I appreciate the simplicity of it all. Nothing special about an error, it’s just part of how you do everything else. But on the other hand, there is something clearly special about an error. It’s something 100% of go users have to deal with in almost every single function call. There is something clearly special about it. These grass…
Rust's is good but not perfect. I often find myself missing stack traces (there are solutions but they're not easy to use), and you're still constrained to a single type of error per function, which means you see a proliferation of specialized error types that are mutually incompatible and have to be converted back and forth.