// Package uerr provides error sentinels that can wrap other errors and carry payloads. // Each error message describes only its current node, excluding causes. package uerr import "slices" // ErrOption configures an error created by a Sentinel or PayloadSentinel. type ErrOption func(*base) // WithCauses sets the errors wrapped by the created error. Nil causes are skipped. func WithCauses(causes ...error) ErrOption { return func(b *base) { for _, cause := range causes { if cause != nil { b.causes = append(b.causes, cause) } } } } // base holds the state shared by all errors created by sentinels. type base struct { causes []error stack []uintptr } // Unwrap returns the causes of the error. The returned slice is shared with the error, // so callers must not modify it. func (b *base) Unwrap() []error { return b.causes } // children returns the non-nil errors wrapped by err. func children(err error) []error { var errs []error switch e := err.(type) { case interface{ Unwrap() []error }: errs = e.Unwrap() case interface{ Unwrap() error }: errs = []error{e.Unwrap()} } if !slices.Contains(errs, nil) { return errs } return slices.DeleteFunc(slices.Clone(errs), func(e error) bool { return e == nil }) }