forked from keybase/go-updater
/
error.go
40 lines (35 loc) · 840 Bytes
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package util
import (
"fmt"
"strings"
)
// removeNilErrors returns error slice with nil errors removed
func removeNilErrors(errs []error) []error {
if len(errs) == 0 {
return nil
}
var r []error
for _, err := range errs {
if err != nil {
r = append(r, err)
}
}
return r
}
// CombineErrors returns a single error for multiple errors, or nil if none
func CombineErrors(errs ...error) error {
errs = removeNilErrors(errs)
if len(errs) == 0 {
return nil
} else if len(errs) == 1 {
return errs[0]
}
// Combine multiple errors
msgs := []string{}
for _, err := range errs {
msgs = append(msgs, err.Error())
}
return fmt.Errorf("There were multiple errors: %s", strings.Join(msgs, "; "))
}