Skip to content

Commit

Permalink
support the As(any) bool interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Weber committed Nov 18, 2024
1 parent 15d7d2a commit a5a573f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ type errorGroup interface {
Errors() []error
}

type asAny interface {
As(any) bool
}

// CodeChain resolves wrapped errors down to the first ErrorCode.
// An error that is a grouping with multiple codes will have its error codes combined to a MultiErrorCode.
// If the given error is not an ErrorCode, a ContextChain will be returned with Top set to the given error.
Expand All @@ -151,7 +155,19 @@ func CodeChain(errInput error) ErrorCode {
checkError := func(err error) ErrorCode {
if errCode, ok := err.(ErrorCode); ok {
return errCode
} else if eg, ok := err.(errorGroup); ok {
}

as, asOK := err.(asAny)
var ecAs ErrorCode
if asOK && as.As(ecAs) {
return ecAs
}

eg, egOK := err.(errorGroup)
if !egOK && asOK && as.As(eg) {
egOK = true
}
if egOK {
group := []ErrorCode{}
for _, errItem := range eg.Errors() {
if itemCode := CodeChain(errItem); itemCode != nil {
Expand Down

0 comments on commit a5a573f

Please sign in to comment.