Home > Backend Development > Golang > Does Go's `errors.Is` and `errors.As` Support Recursive Error Wrapping?

Does Go's `errors.Is` and `errors.As` Support Recursive Error Wrapping?

Patricia Arquette
Release: 2024-12-14 19:23:10
Original
534 people have browsed it

Does Go's `errors.Is` and `errors.As` Support Recursive Error Wrapping?

Recursive Error Wrapping in Go

Go's error handling mechanism provides methods such as Is() and As() for error inspection. However, it is often assumed that these methods support recursive error wrapping. This is not entirely true.

Using fmt.Errorf with the %w verb only allows for shallow error wrapping. To achieve true recursion, a custom error type is necessary.

Typically, a custom error type would include a pointer to the wrapped error as well as a method to unwrap it. However, simply wrapping errors in this manner introduces a problem. If multiple wrapped errors share the same pointer, error comparison will fail since Go errors are compared by address.

A solution is to implement Is() and As() methods within the custom error type. These methods should compare the embedded error instead of the custom type itself, allowing for full recursive error wrapping without address-based issues.

For example, here is a simplified version of an error type that supports recursive wrapping:

type errorChain struct {
    err  error
    next *errorChain
}

func (c errorChain) Is(err error) bool { return errors.Is(c.err, err) }
func (c errorChain) As(target any) bool { return errors.As(c.err, target) }
func (c errorChain) Unwrap() error  { return c.next }
Copy after login

This error type can then be used to wrap errors as desired, allowing for full error inspection using Is() and As().

The above is the detailed content of Does Go's `errors.Is` and `errors.As` Support Recursive Error Wrapping?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template