Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
Latest commit
192 lines (157 loc) · 6.24 KB
/
Copy patherror.go
File metadata and controls
192 lines (157 loc) · 6.24 KB
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
package structurederrors
import (
"fmt"
"os"
"reflect"
"strings"
"github.com/golang/glog"
"github.com/kr/pretty"
"github.com/kr/text"
)
varverboseErrors=false
funcSetVerboseErrors(verbosebool) {
verboseErrors=verbose
}
funcExitWithErr(msginterface{}) {
glog.Error(msg)
os.Exit(1)
}
funcUsageErrorf(commandPath, finterface{}, args...interface{}) error {
returnerrorf(fmt.Sprintf("See '%s -h' for help and examples", commandPath), f, args...)
}
// TypeError means that obj has an unexpected type.
funcTypeError(objinterface{}) error {
returnfmt.Errorf("unrecognized type (%s)", reflect.TypeOf(obj))
}
// TypeErrorf is like TypeError, except with a custom message.
funcTypeErrorf(objinterface{}, msgFormatstring, args...interface{}) error {
returnerrorf(pretty.Sprintf(msgFormat, args...), "unrecognized type (%s)", reflect.TypeOf(obj))
}
// TypeContextErrorf is like TypeErrorf, but it adds its message as context to an existing error.
funcTypeContextErrorf(baseErrorerror, objinterface{}, msgFormatstring, args...interface{}) *ErrorWithContext {
msg:=pretty.Sprintf(msgFormat, args)
typeMsg:=pretty.Sprintf("unrecognized type (%s)", reflect.TypeOf(obj))
returnContextualizeErrorf(baseError, "%s: %s", msg, typeMsg)
}
// InvalidInstanceError means that obj is the correct type, but there's something
// wrong with its contents.
funcInvalidInstanceError(objinterface{}) error {
returninstanceError(obj, "unrecognized instance")
}
// InvalidInstanceErrorf is like InvalidInstanceError, except with a custom message.
funcInvalidInstanceErrorf(objinterface{}, msgFormatstring, args...interface{}) error {
returninstanceError(obj, pretty.Sprintf(msgFormat, args...))
}
// InvalidInstanceContextErrorf is like InvalidInstanceContextErrorf, except it adds its message as context to an existing error.
funcInvalidInstanceContextErrorf(baseErrorerror, objinterface{}, msgFormatstring, args...interface{}) *ErrorWithContext {
returnContextualizeErrorf(baseError, InvalidInstanceErrorf(obj, msgFormat, args...).Error())
}
// InvalidValueErrorf is used when the type isn't meaningful--just the contents and the
// context matter.
funcInvalidValueErrorf(valinterface{}, msgFormatstring, args...interface{}) error {
ifverboseErrors {
returnpretty.Errorf("%s\n(%# v)", pretty.Sprintf(msgFormat, args...), val)
}
returnpretty.Errorf("(%s) value: %s", reflect.TypeOf(val), pretty.Sprintf(msgFormat, args...))
}
funcInvalidValueContextErrorf(baseErrorerror, valinterface{}, msgFormatstring, args...interface{}) *ErrorWithContext {
returnContextualizeErrorf(baseError, InvalidValueErrorf(val, msgFormat, args...).Error())
}
// InvalidValueForTypeError is used when the type isn't meaningful--just the contents and the
// context matter.
funcInvalidValueForTypeError(val, typedObjinterface{}) error {
ifverboseErrors {
returnpretty.Errorf("for type (%s), unrecognized value\n%# v", reflect.TypeOf(typedObj), val)
}
returnpretty.Errorf("for type (%s), unrecognized (%s) value", reflect.TypeOf(typedObj), reflect.TypeOf(val))
}
funcInvalidValueForTypeContextError(baseErrorerror, val, typedObjinterface{}) *ErrorWithContext {
returnContextualizeErrorf(baseError, InvalidValueForTypeError(val, typedObj).Error())
}
// InvalidValueForTypeErrorf is used when the type isn't meaningful--just the contents and the
// context matter.
funcInvalidValueForTypeErrorf(val, typedObjinterface{}, msgFormatstring, args...interface{}) error {
ifverboseErrors {
returnpretty.Errorf("for type (%s), unrecognized value\n%s\n%# v", reflect.TypeOf(typedObj), pretty.Sprintf(msgFormat, args...), val)
}
returnpretty.Errorf("for type (%s), unrecognized (%s) value: %s", reflect.TypeOf(typedObj), reflect.TypeOf(val), pretty.Sprintf(msgFormat, args...))
}
funcInvalidValueForTypeContextErrorf(baseErrorerror, val, typedObjinterface{}, msgFormatstring, args...interface{}) *ErrorWithContext {
returnContextualizeErrorf(baseError, InvalidValueForTypeErrorf(val, typedObj, msgFormat, args...).Error())
}
typeErrorWithContextstruct {
BaseErrorerror
Context []string
}
func (e*ErrorWithContext) Error() string {
context:=ReversedStringsList(e.Context)
returnstrings.Join(append(context, e.BaseError.Error()), ": ")
}
func (e*ErrorWithContext) PrettyError() string {
context:=make([]string, len(e.Context))
indent:=""
fori, contextItem:=rangeReversedStringsList(e.Context) {
context[i] =text.Indent(contextItem, indent)
indent=indent+" "
}
contextString:=strings.Join(context, "\n")
errString:=text.Indent(PrettyError(e.BaseError), indent)
returnfmt.Sprintf("%s\n%s", contextString, errString)
}
// ContextualizeErrorf is for adding an additional message to an existing error.
// This method is only intended for simple messages (contextFormat).
// e.g. If the context includes a printout of a Go struct, use one of the other error generators in this package.
funcContextualizeErrorf(errerror, contextFormatstring, contextArgs...interface{}) *ErrorWithContext {
contextMsg:=pretty.Sprintf(contextFormat, contextArgs...)
switcherr:=err.(type) {
case*ErrorWithContext:
err.Context=append(err.Context, pretty.Sprintf(contextFormat, contextArgs...))
returnerr
default:
return&ErrorWithContext{
BaseError: err,
Context: []string{contextMsg},
}
}
}
funcPrettyError(errerror) string {
switcherr:=err.(type) {
case*ErrorWithContext:
returnerr.PrettyError()
default:
returnerr.Error()
}
}
funcinstanceError(objinterface{}, msgstring) error {
ifverboseErrors {
returnpretty.Errorf("%s: %s\n(%# v)", reflect.TypeOf(obj), msg, obj)
}
returnpretty.Errorf("%s: %s", reflect.TypeOf(obj), msg)
}
funcerrorf(addedMsg, finterface{}, args...interface{}) error {
format:=""
switchf:=f.(type) {
casestring:
format=f
case fmt.Stringer:
format=f.String()
caseerror:
format=f.Error()
default:
glog.Errorf("unrecognized format type %v", f)
}
msg:=pretty.Sprintf(format, args...)
returnfmt.Errorf("%s\n %s", msg, addedMsg)
}
funcReversedStringsList(stringsList []string) []string {
ifstringsList==nil {
returnnil
}
l:=len(stringsList)
reversed:=make([]string, l)
fori, str:=rangestringsList {
j:=l-1-i
reversed[j] =str
}
returnreversed
}