-
Notifications
You must be signed in to change notification settings - Fork 9
/
multierr.go
122 lines (108 loc) · 3 KB
/
multierr.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// Package multierr provides a simple multiple-error type. It is a fork of
// github.com/tailscale/tailscale/util/multierr that is compatible with Go 1.17.
package multierr
import (
"errors"
"strings"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"namespacelabs.dev/foundation/schema/tasks"
)
// An Error represents multiple errors.
type Error struct {
errs []error
}
func (e Error) GRPCStatus() *status.Status {
statuses := make([]*status.Status, len(e.errs))
protos := make([]*spb.Status, len(e.errs))
for k, err := range e.errs {
statuses[k], _ = status.FromError(err)
protos[k] = statuses[k].Proto()
}
st := status.New(dominantCode(statuses), e.Error())
p, _ := st.WithDetails(&tasks.ErrorDetail_OriginalErrors{Status: protos})
return p
}
func dominantCode(statuses []*status.Status) codes.Code {
if len(statuses) == 0 {
return codes.Unknown
}
expected := statuses[0].Code()
for i := 1; i < len(statuses); i++ {
if expected != statuses[i].Code() {
return codes.Unknown
}
}
return expected
}
// Error implements the error interface.
func (e Error) Error() string {
s := new(strings.Builder)
s.WriteString("multiple errors:")
for _, err := range e.errs {
s.WriteString("\n\t")
s.WriteString(err.Error())
}
return s.String()
}
// Errors returns a slice containing all errors in e.
func (e Error) Errors() []error {
return append(e.errs[:0:0], e.errs...)
}
// New returns an error composed from errs.
// Some errors in errs get special treatment:
// - nil errors are discarded
// - errors of type Error are expanded into the top level
//
// If the resulting slice has length 0, New returns nil.
// If the resulting slice has length 1, New returns that error.
// If the resulting slice has length > 1, New returns that slice as an Error.
func New(errs ...error) error {
dst := make([]error, 0, len(errs))
for _, e := range errs {
switch e := e.(type) {
case nil:
continue
case Error:
dst = append(dst, e.errs...)
default:
dst = append(dst, e)
}
}
// dst has been filtered and splatted.
switch len(dst) {
case 0:
return nil
case 1:
return dst[0]
}
// Zero any remaining elements of dst left over from errs, for GC.
tail := dst[len(dst):cap(dst)]
for i := range tail {
tail[i] = nil
}
return Error{errs: dst}
}
// Is reports whether any error in e matches target.
func (e Error) Is(target error) bool {
for _, err := range e.errs {
if errors.Is(err, target) {
return true
}
}
return false
}
// As finds the first error in e that matches target, and if any is found,
// sets target to that error value and returns true. Otherwise, it returns false.
func (e Error) As(target interface{}) bool {
for _, err := range e.errs {
if ok := errors.As(err, target); ok {
return true
}
}
return false
}