Go Error Wrapping: Difference between revisions
Line 37: | Line 37: | ||
=Inspect the Error Tree= | =Inspect the Error Tree= | ||
==<tt>errors.Is()</tt>== | ==<tt>errors.Is()</tt>== | ||
{{External|https://pkg.go.dev/errors#Is}} | |||
The presence of a wrapped error inside an outer error, as a direct or indirect descendant, can be confirmed with <code>errors.Is(<outer_error>, <sought_for_error>)</code> function: | The presence of a wrapped error inside an outer error, as a direct or indirect descendant, can be confirmed with <code>errors.Is(<outer_error>, <sought_for_error>)</code> function: | ||
Revision as of 20:50, 28 December 2023
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.
The pattern consists in wrapping an error returned by the underlying layer into an "outer" error instance. This is typically done to add information relevant to the context that caught the error being processed. However, wrapping the error is more than error annotation, because it involves embedding an actual error instance, preserving its unique identity, instead of concatenating strings.
Wrap the Error
Individual errors are typically wrapped with fmt.Errorf()
. To wrap multiple error instances into a single outer error, use errors.Join()
.
Wrap an Individual Error
A typical error handling pattern is to handle an error returned by a function invocation by "wrapping" it into an outer error instance that provides additional context, in form of an addition error text message.The approach is different from annotating the error because it creates a new error instance while preserving the identity of the wrapped error. The wrapped error is still reachable by introspecting the error tree with methods like errors.Is()
or errors.As()
.
The error is wrapped with fmt.Errorf()
and %w
conversion character:
if err != nil {
outer := fmt.Errorf("additional context: %w", err)
return outer
}
fmt.Errorf()
call creates a new error instance whose Error()
method concatenates the error message provided as argument and the inner error message:
additional context: original context
The outer error assumes the identity of the inner error, which can be checked with errors.Is()
function:
errors.Is(outer, err) // returns true
The outer error becomes the direct ancestor of the wrapped error, and successive invocations of the fmt.Errorf()
result in an error tree, where the identity of the wrapped errors can be checked with errors.Is()
. More details on errors.Is()
can be found below.
errors.Join()
Join returns an error that wraps the given errors. Any nil
error values are discarded. Join returns nil
if every value provided as argument is nil
. The error formats as the concatenation of the strings obtained by calling the Error()
method of each element, with a newline between each string.
Inspect the Error Tree
errors.Is()
The presence of a wrapped error inside an outer error, as a direct or indirect descendant, can be confirmed with errors.Is(<outer_error>, <sought_for_error>)
function:
orig := fmt.Errorf("original error")
intermediate := fmt.Errorf("intermediate error: %w", orig)
outer := fmt.Errorf("final intermediate error: %w", intermediate)
println(errors.Is(outer, intermediate)) // prints "true"
println(errors.Is(outer, orig)) // prints "true"
println(errors.Is(intermediate, orig)) // prints "true"
errors.As()
TO DEPLETE
Checking for Wrapped Errors
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?