February 2021 Online: Error handling discussion

Meetup #71. Please RSVP at Meetup.

We will be meeting online using this Google Meet at 6:30pm MST.

Talks

Error Handling discussion

Peter Preeper walked through how Go 1.13 changed error handling, building on the errors-as-values approach. The session contrasted the old style — exact comparisons to sentinel errors and type assertions for custom error types — with the new errors.Is and errors.As, which inspect the whole wrapping chain in a single call. He demonstrated fmt.Errorf's new %w verb, which wraps an error value (so errors.Is/errors.As can still find it), versus %v, which only concatenates the text and discards the underlying value; he also showed errors.Unwrap and multi-level wrapping. The discussion closed on when not to wrap: wrapping makes the underlying error part of your API (for example exposing sql.ErrNoRows from a LookupUser function), so avoid wrapping when it leaks implementation details. The group then watched "Can We Panic Yet? Error Handling in Go" together and discussed it.

Key points:

  • Go 1.13's errors.Is/errors.As inspect the entire wrapped chain.
  • %w in fmt.Errorf wraps an error value; %v only copies text.
  • errors.Unwrap peels back wrapped errors.
  • errors.As recovers a concrete error type from the chain.
  • Wrapping an error makes it part of your API contract — wrap deliberately.
  • Don't wrap when it would expose implementation details.
  • The session included watching and discussing "Can We Panic Yet?".

presentation

News

Events

  • Edmonton Go Meetup - April