Go Error Wrapping

From NovaOrdis Knowledge Base
Revision as of 20:06, 28 December 2023 by Ovidiu (talk | contribs) (→‎TO DEPLETE)
Jump to navigation Jump to search

Internal

Overview

Go error mechanism allows "wrapping" error instances into other error instances, while preserving the wrapped error identity. This pattern supports building an error tree that is useful in preserving context.

Error wrapping and returning the result to the upper layer is one of the common error handling patterns in Go. The others are fully handling the error without returning it, simply returning it without any modification, and returning a new annotated error.

Wrap the Error

Inspect the Error Tree

TO DEPLETE

An error instance can be wrapped with fmt.Errorf() and %w conversion character.

if err != nil {
  return fmt.Errorf("additional information: %w", err)
}

The wrapped error message, assuming that the original error is "initial information", with be:

additional information: initial information

Document errors.Unwrap()

Checking for Wrapped Errors

A wrapped error can be identified in an enclosing error with the errors.Is(<outer_error>, <sought_for_error>) function.

Need to understand errors.Is(), error tree, wrapping and unwrapping.

var BlueError = errors.New("some information")
var GreenError = errors.New("some information")

...

// wrap the error in an outer error
outerError := fmt.Errorf("addtional info: %w", BlueError)

if errors.Is(outerError, BlueError) {
  fmt.Println("found blue error")
}

if errors.Is(outerError, GreenError) {
  fmt.Println("found green error")
}

BlueError is correctly identified, even though both BlueError and GreenError carry the same string. How?