4 ms·
Snip: if err != nil { return fmt.Errorf("invalid integer: %q", a) } Snip. No syntax for error handling is OK with me. Spelling out "hey I actu
by rollcat 1y ago
Snip:
if err != nil {
return fmt.Errorf("invalid integer: %q", a)
}
Snip. No syntax for error handling is OK with me. Spelling out "hey I actually do need a stack trace" at every call site isn't.
- skydhash 1y agoYou actually don't. In mistakes #52 "Handling an error twice" from the book "100 Go Mistakes and How to Avoid Them" by Teiva Harsanyi, the recommendation is to either return an error to the caller or handle the situation (and maybe logging it). Sometimes you wnat some extra information as to why it's an error, so you wrap it. Go forces you to be explicit about error handling. Java syntax is not that much better. JavaScript, Kotlin, Swift,... is more about avoiding null pointer exception than proper error handling.
- rollcat 1y agoI return the error to the caller. The caller returns it to their caller. 5 frames up, someone gets a [syscall.EINVAL], and has to figure out what to do about it. Perhaps it's time to log it? If I had to write my own "100 mistakes" book, "assuming the callee knows what to do" would be somewhere in the top 20, down below "I won't need to debug this".
- skydhash 1y agoIt's all about designing software. The callee is the one encountering the error, not any of the caller up in the stack trace. Somewhere in the call chain, there's a need to take a decision. So you, as the developer, decide where that needs to be. It may be at the callee level (like an exponential retry) or at the caller level (display an error message). In the later case, you may want to add more information to the error data block, so that the caller my handle the situation appropriately. So if you want tracing, you just need to wrap the error and returns it. Then your logging code have all the information it needs: like [error saving file [permission error [can't access file]]] instead of just [syscall.EINVAL].
- rollcat 1y ago> So if you want tracing, you just need to wrap the error and returns it. Here we go, fifth time we're both spelling this one out. This thread is now a meta-self-joke.