-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
292 lines (236 loc) · 7.13 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//go:generate go run github.com/princjef/gomarkdoc/cmd/gomarkdoc ./pkg/... --output "{{.Dir}}/README.md" -e
package errors
import (
"context"
"fmt"
"log"
"github.com/error-fyi/go-fyi/pkg/errorclient"
"github.com/error-fyi/go-fyi/pkg/errorclient/local"
)
type (
// HandlerOption is a more atomic to configure the different HandlerOptions rather than passing the entire ErrClientOptions struct.
HandlerOption func(o *HandlerOptions)
// HandlerOptions contains all the different configuration values available to the wrapper
HandlerOptions struct {
// Logger is internal Logger for the wrapper, leave nil to avoid logging
Logger *log.Logger
// Silent globally sets the error handler to stop adding the additional error context in the input error of Error() and ErrorWithContext()
Silent bool
errorclient.ErrClientOptions
}
// Handler is the wrapper struct for errors
Handler struct {
Options *HandlerOptions
}
)
var (
global = New()
)
const (
defaultErrorSpecificationLocation = "./errors.yaml"
)
/*
New creates and configures a new instance of the error handler.
example:
errHandler := New(WithManifest("content"),WithNoLogger(),WithRenderMarkdown(true))
errHandler.Error(err, "error code")
*/
func New(opts ...HandlerOption) *Handler {
newOpts := &HandlerOptions{
log.Default(),
false,
errorclient.ErrClientOptions{
SourceFilename: defaultErrorSpecificationLocation,
Source: nil,
ErrorDefinitionURLPath: "",
DisplayMarkdownErrors: true,
NumberOfSuggestions: 1,
OverrideErrorURL: "",
DisplayShortSummary: true,
DisplayErrorURL: true,
},
}
for _, opt := range opts {
opt(newOpts)
}
for _, opt := range opts {
opt(newOpts)
}
return &Handler{
Options: newOpts,
}
}
// SetManifest sets the source manifest used by the Handler
// note: it is not required if the SetManifestFilename is set
func (w *Handler) SetManifest(content []byte) {
w.Options.Source = content
}
// SetManifestFilename sets the file path of the manifest used by the Handler.
// note: it is not required if SetManifest is set
func (w *Handler) SetManifestFilename(filepath string) {
w.Options.SourceFilename = filepath
}
// SetLogger sets the logger used by the Handler internally.
// note: pass nil if no logging is wanted
func (w *Handler) SetLogger(logger *log.Logger) {
w.Options.Logger = logger
}
// SetErrorParentPath
func (w *Handler) SetErrorParentPath(parentDir string) {
w.Options.ErrorDefinitionURLPath = parentDir
}
// SetSilence
func (w *Handler) SetSilence(silence bool) {
w.Options.Silent = silence
}
// SetDisplayedSuggestions
func (w *Handler) SetDisplayedSuggestions(num int) {
w.Options.NumberOfSuggestions = num
}
// SetMarkdownRender
func (w *Handler) SetMarkdownRender(markdown bool) {
w.Options.DisplayMarkdownErrors = markdown
}
// SetOverrideErrorURL
func (w *Handler) SetOverrideErrorURL(url string) {
w.Options.OverrideErrorURL = url
}
// SetShowShortSummary
func (w *Handler) SetShowShortSummary(flag bool) {
w.Options.DisplayShortSummary = flag
}
// SetDisplayErrorURL
func (w *Handler) SetDisplayErrorURL(flag bool) {
w.Options.DisplayErrorURL = flag
}
// ErrorWithContext wraps the incoming error with error defined by the Aloe specification according to the input code.
// if no error is found in the specification, the original error is returned.
func (w *Handler) ErrorWithContext(ctx context.Context, err error, code string, opts ...HandlerOption) error {
if w == nil || err == nil {
return err
}
currentOpts := w.Options
for _, opt := range opts {
opt(currentOpts)
}
client := local.New(currentOpts.ErrClientOptions)
if !currentOpts.Silent {
newErrMessage, genErr := client.GenerateErrorMessageFromCode(ctx, code)
if genErr != nil {
w.log(genErr.Error())
return err
}
return fmt.Errorf("[%w]\n%s", err, newErrMessage)
}
return err
}
// Error wraps the incoming error with error defined by the application error manifest according to the input code.
// if no error is found in the application error manifest, the original error is returned.
func (w *Handler) Error(err error, code string, opts ...HandlerOption) error {
return w.ErrorWithContext(context.Background(), err, code, opts...)
}
func (w *Handler) log(msg string, keyVal ...any) {
if w.Options.Logger != nil {
w.Options.Logger.Printf(msg, keyVal...)
}
}
// Global Handler Functions //
func SetManifest(content []byte) {
global.SetManifest(content)
}
func SetManifestFilename(filepath string) {
global.SetManifestFilename(filepath)
}
func SetLogger(logger *log.Logger) {
global.SetLogger(logger)
}
func SetErrorParentPath(parentDir string) {
global.SetErrorParentPath(parentDir)
}
func SetSilence(silence bool) {
global.SetSilence(silence)
}
func SetDisplayedSuggestions(num int) {
global.SetDisplayedSuggestions(num)
}
func SetMarkdownRender(markdown bool) {
global.SetMarkdownRender(markdown)
}
// SetOverrideErrorURL
func SetOverrideErrorURL(url string) {
global.Options.OverrideErrorURL = url
}
// SetShowShortSummary
func SetShowShortSummary(flag bool) {
global.Options.DisplayShortSummary = flag
}
// SetDisplayErrorURL
func SetDisplayErrorURL(flag bool) {
global.Options.DisplayErrorURL = flag
}
// ErrorWithContext wraps the incoming error with error defined by the application error manifest according to the input code.
// if no error is found in the application error manifest, the original error is returned.
func ErrorWithContext(ctx context.Context, err error, code string, opts ...HandlerOption) error {
return global.ErrorWithContext(ctx, err, code, opts...)
}
// Error wraps the incoming error with error defined by the application error manifest according to the input code.
// if no error is found in the application error manifest, the original error is returned.
func Error(err error, code string, opts ...HandlerOption) error {
return global.Error(err, code, opts...)
}
// HandlerOption Functions //
func WithManifest(source []byte) HandlerOption {
return func(o *HandlerOptions) {
o.Source = source
}
}
func WithManifestFilename(filename string) HandlerOption {
return func(o *HandlerOptions) {
o.SourceFilename = filename
}
}
func WithLogger(logger *log.Logger) HandlerOption {
return func(o *HandlerOptions) {
o.Logger = logger
}
}
func WithNoLogger() HandlerOption {
return func(o *HandlerOptions) {
o.Logger = nil
}
}
func WithErrorParentPath(parentDir string) HandlerOption {
return func(o *HandlerOptions) {
o.ErrorDefinitionURLPath = parentDir
}
}
func WithSilence(silence bool) HandlerOption {
return func(o *HandlerOptions) {
o.Silent = silence
}
}
func WithNumberOfSuggestions(num int) HandlerOption {
return func(o *HandlerOptions) {
o.NumberOfSuggestions = num
}
}
func WithRenderMarkdown(markdown bool) HandlerOption {
return func(o *HandlerOptions) {
o.DisplayMarkdownErrors = markdown
}
}
func WithOverrideErrorURL(url string) HandlerOption {
return func(o *HandlerOptions) {
o.OverrideErrorURL = url
}
}
func WithShowShortSummary(flag bool) HandlerOption {
return func(o *HandlerOptions) {
o.DisplayShortSummary = flag
}
}
func WithDisplayErrorURL(flag bool) HandlerOption {
return func(o *HandlerOptions) {
o.DisplayErrorURL = flag
}
}