The Wire

Error Handling Habits

Errors carry meaning. Wrapping them well is a form of documentation.

Errors in Go are values, which means they deserve the same care as any other part of the API. A wrapped error should tell the caller what failed and preserve the original cause. A sentinel error should be stable enough to compare against. A message should be written for the person who will read it in a log at an inconvenient hour.

The habit that pays off most is deciding early which errors are expected and which are exceptional. Expected errors become part of the returned contract; exceptional ones bubble up with context. Once that distinction is clear, retries, user-facing messages, and tests all become easier to design.

Arrived from