forked from kptdev/kpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
272 lines (235 loc) · 6.52 KB
/
errors.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2021 The kpt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package errors defines the error handling used by kpt codebase.
package errors
import (
"fmt"
"strings"
goerrors "errors"
"github.com/GoogleContainerTools/kpt/internal/types"
kyaml_errors "github.com/go-errors/errors"
)
// Error is the type that implements error interface used in the kpt codebase.
// It is based on the design in https://commandcenter.blogspot.com/2017/12/error-handling-in-upspin.html
// The intent is to capture error information in a structured format so
// that we can display it differently to different users for ex. kpt developers
// are interested in operational trace along with more diagnostic information while
// kpt end-users may be just interested in a concise and actionable information.
// Representing errors in structured format helps us decouple the error information
// from how it is surfaced to the end users.
type Error struct {
// Path is the path of the object (pkg, file) involved in kpt operation.
Path types.UniquePath
// Op is the operation being performed, for ex. pkg.get, fn.render
Op Op
// Fn is the kpt function being run either as part of "fn render" or "fn eval"
Fn Fn
// Repo is the git repo used for get, update or diff
Repo Repo
// Class refers to class of errors
Class Class
// Err refers to wrapped error (if any)
Err error
}
func (e *Error) Error() string {
b := new(strings.Builder)
if e.Op != "" {
pad(b, ": ")
b.WriteString(string(e.Op))
}
if e.Path != "" {
pad(b, ": ")
b.WriteString("pkg ")
// try to print relative path of the pkg if we can else use abs path
relPath, err := e.Path.RelativePath()
if err != nil {
relPath = string(e.Path)
}
b.WriteString(relPath)
}
if e.Fn != "" {
pad(b, ": ")
b.WriteString("fn ")
b.WriteString(string(e.Fn))
}
if e.Repo != "" {
pad(b, ": ")
b.WriteString("repo ")
b.WriteString(string(e.Repo))
}
if e.Class != 0 {
pad(b, ": ")
b.WriteString(e.Class.String())
}
if e.Err != nil {
var wrappedErr *Error
if As(e.Err, &wrappedErr) {
if !wrappedErr.Zero() {
pad(b, ":\n\t")
b.WriteString(wrappedErr.Error())
}
} else {
pad(b, ": ")
b.WriteString(e.Err.Error())
}
}
if b.Len() == 0 {
return "no error"
}
return b.String()
}
// pad appends given str to the string buffer.
func pad(b *strings.Builder, str string) {
if b.Len() == 0 {
return
}
b.WriteString(str)
}
func (e *Error) Zero() bool {
return e.Op == "" && e.Path == "" && e.Fn == "" && e.Class == 0 && e.Err == nil
}
func (e *Error) Unwrap() error {
return e.Err
}
// Op describes the operation being performed.
type Op string
// Fn describes the kpt function associated with the error.
type Fn string
// Repo describes the repo associated with the error.
type Repo string
// Class describes the class of errors encountered.
type Class int
const (
Other Class = iota // Unclassified. Will not be printed.
Exist // Item already exists.
Internal // Internal error.
InvalidParam // Value is not valid.
MissingParam // Required value is missing or empty.
Git // Errors from Git
IO // Error doing IO operations
YAML // yaml document can't be parsed
)
func (c Class) String() string {
switch c {
case Other:
return "other error"
case Exist:
return "item already exist"
case Internal:
return "internal error"
case InvalidParam:
return "invalid parameter value"
case MissingParam:
return "missing parameter value"
case Git:
return "git error"
case IO:
return "IO error"
case YAML:
return "yaml parsing error"
}
return "unknown kind"
}
func E(args ...interface{}) error {
if len(args) == 0 {
panic("errors.E must have at least one argument")
}
e := &Error{}
for _, arg := range args {
switch a := arg.(type) {
case types.UniquePath:
e.Path = a
case Op:
e.Op = a
case Fn:
e.Fn = a
case Repo:
e.Repo = a
case Class:
e.Class = a
case *Error:
cp := *a
e.Err = &cp
case error:
e.Err = a
case string:
e.Err = fmt.Errorf(a)
default:
panic(fmt.Errorf("unknown type %T for value %v in call to error.E", a, a))
}
}
wrappedErr, ok := e.Err.(*Error)
if !ok {
return e
}
if e.Path == wrappedErr.Path {
wrappedErr.Path = ""
}
if e.Op == wrappedErr.Op {
wrappedErr.Op = ""
}
if e.Fn == wrappedErr.Fn {
wrappedErr.Fn = ""
}
if e.Repo == wrappedErr.Repo {
wrappedErr.Repo = ""
}
if e.Class == wrappedErr.Class {
wrappedErr.Class = 0
}
return e
}
// Is reports whether any error in err's chain matches target.
func Is(err, target error) bool {
return goerrors.Is(err, target)
}
// As finds the first error in err's chain that matches target, and if so, sets
// target to that error value and returns true. Otherwise, it returns false.
func As(err error, target interface{}) bool {
return goerrors.As(err, target)
}
// UnwrapKioError unwraps the error returned by kio pipeline.
// The error returned by kio pipeline is wrapped by
// library 'github.com/go-errors/errors' and it doesn't
// support 'Unwrap' method so 'errors.As' will not work.
// This function will return the first error wrapped by kio
// pipeline. If the error is not wrapped by kio pipeline, it
// will return the original error.
func UnwrapKioError(err error) error {
var kioErr *kyaml_errors.Error
if !goerrors.As(err, &kioErr) {
return err
}
return kioErr.Err
}
// UnwrapErrors unwraps any *Error errors in the chain and returns
// the first error it finds that is of a different type. If no such error
// can be found, the last return value will be false.
// nolint
func UnwrapErrors(err error) (error, bool) {
for {
if err == nil {
return nil, false
}
e, ok := err.(*Error)
if !ok {
return err, true
}
err = e.Err
}
}
// ErrAlreadyHandled is an error that is already handled by
// a kpt command and nothing needs to be done by the global
// error handler except to return a non-zero exit code.
var ErrAlreadyHandled = fmt.Errorf("already handled error")