A Palatable Javascript Pattern
blog.j15r.com7 pointsby joelgwebber1 comments
// You write something like this:
result, err := getResults()
if err != nil {
return err
}
// Then you reuse err for the next call
result, err := getResults()
if err != nil {
return err
}
stats, err := computeStats(result)
if err != nil {
return err
}
Eventually, you realize that `return err` just isn't enough most of the time, because it's impossible to make sense out of your logs. So we added a simple "error chaining" function that allows you to pass context when you return the error, which makes the logs much clearer than just a single error message, or raw stack trace. And of course it is often the case that you want to do more logging in your error blocks, even as you ignore the error and move on (e.g., "spurious error reading memcache entry; falling back to the slow thing").