Skip to content

Commit

Permalink
Remove multiError and use errors.Join
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Feb 14, 2024
1 parent fd24e7e commit 44ad4a0
Showing 1 changed file with 5 additions and 49 deletions.
54 changes: 5 additions & 49 deletions common/remote_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"regexp"
Expand Down Expand Up @@ -39,51 +40,6 @@ func (pe *objectPropertyParseError) Unwrap() error {
return pe.error
}

type multiError struct {
Errors []error
}

func (me *multiError) append(err error) {
me.Errors = append(me.Errors, err)
}

func (me multiError) Error() string {
if len(me.Errors) == 0 {
return ""
}
if len(me.Errors) == 1 {
return me.Errors[0].Error()
}

var buf strings.Builder
fmt.Fprintf(&buf, "%d errors occurred:\n", len(me.Errors))
for _, e := range me.Errors {
fmt.Fprintf(&buf, "\t* %s\n", e)
}

return buf.String()
}

func multierror(err error, errs ...error) error {
me := &multiError{}
// We can't use errors.As(), as we want to know if err is of type
// multiError, not any error in the chain. If err contains a wrapped
// multierror, start a new multiError that will contain err.
e, ok := err.(*multiError) //nolint:errorlint

if ok {
me = e
} else if err != nil {
me.append(err)
}

for _, e := range errs {
me.append(e)
}

return me
}

type remoteObjectParseError struct {
error
typ string
Expand All @@ -104,21 +60,21 @@ func (e *remoteObjectParseError) Unwrap() error {

func parseRemoteObjectPreview(op *cdpruntime.ObjectPreview) (map[string]any, error) {
obj := make(map[string]any)
var result error
var result []error
if op.Overflow {
result = multierror(result, &objectOverflowError{})
result = append(result, &objectOverflowError{})
}

for _, p := range op.Properties {
val, err := parseRemoteObjectValue(p.Type, p.Subtype, p.Value, p.ValuePreview)
if err != nil {
result = multierror(result, &objectPropertyParseError{err, p.Name})
result = append(result, &objectPropertyParseError{err, p.Name})
continue
}
obj[p.Name] = val
}

return obj, result
return obj, errors.Join(result...)
}

//nolint:cyclop
Expand Down

0 comments on commit 44ad4a0

Please sign in to comment.