Created
November 16, 2022 14:30
-
-
Save byrnedo/581ce55ba7024b9e4032f22aa04543e2 to your computer and use it in GitHub Desktop.
Multierror.go, mutliple go error container.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MultiError don't use directly, use NewMultiError | |
type MultiError map[string]error | |
// NewMultiError returns nil if all errs are nil | |
func NewMultiError(errs map[string]error) error { | |
for k, v := range errs { | |
if v == nil { | |
delete(errs, k) | |
} | |
} | |
if len(errs) == 0 { | |
return nil | |
} | |
return MultiError(errs) | |
} | |
func (m MultiError) Error() string { | |
var errStrs []string | |
for k, v := range m { | |
errStrs = append(errStrs, fmt.Sprintf("%s: %s", k, v.Error())) | |
} | |
if len(errStrs) == 0 { | |
panic("multi error used without errors") | |
} | |
return strings.Join(errStrs, ".\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment