Skip to content

Error Wrapping Best Practices

Traun Leyden edited this page Nov 20, 2017 · 3 revisions
  1. Wrap things at the library boundary rather than internally within the codebase -- for example, a call out to GoCB. This should avoid double-wrapping.
  2. Always use Cause() when comparing error types/values, eg change if err == couchbase.UpdateCancel -> if Cause(err) == couchbase.UpdateCancel.
  3. When wrapping an error, beware that it is a risky operation that can break code that compares errors types or values. Look at callers and make sure they are calling Cause().
  4. Avoid wrapping any errors in any performance critical path due to the performance hit from grabbing the stack frames.
  5. Avoid double-wrapping for performance hit reasons, as well as stack clarity. However there are some exceptions:
    1. In certain cases when a higher-up stack frame can add valuable context
    2. When the error is known to be a fatal error
Clone this wiki locally