Skip to content

Instantly share code, notes, and snippets.

@netbrain
Created April 9, 2021 07:04
Show Gist options
  • Select an option

  • Save netbrain/0861c60db3dc0ccca6a08f4f688891b7 to your computer and use it in GitHub Desktop.

Select an option

Save netbrain/0861c60db3dc0ccca6a08f4f688891b7 to your computer and use it in GitHub Desktop.
multiple errors
type errs []error
func (e errs) Error() string {
s := make([]string,len(e))
for i, err := range e {
s[i] = err.Error()
}
return strings.Join(s,",")
}
func (e errs) Unwrap() error {
err := e[1:]
if len(err) == 0 {
return nil
}
if len(err) == 1 {
return err[0]
}
return err
}
func (e errs) Is(err error) bool {
for _, ee := range e {
if errors.Is(ee,err){
return true
}
}
return false
}
func (e errs) As(target interface{}) bool {
for _, ee := range e {
if errors.As(ee, &target){
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment