-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
214 lines (172 loc) · 5.46 KB
/
factory.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
package falta
import (
"errors"
"fmt"
"regexp"
"strings"
"text/template"
)
// Factory is an error factory.
type Factory[T any] interface {
error
New(vs ...T) Falta
}
// ExtendableFactory is an error factory that can be extended.
type ExtendableFactory[T any] interface {
Factory[T]
Extend(f Factory[T]) ExtendableFactory[T]
}
// Falta is an error returned by the Factory
type Falta struct {
errFmt string
wrappedErr error
error
}
// NewError returns a new Falta error type with the provided error string.
//
// NOTE (a20r, 2024-02-25): It panics if the provided message contains any fmt verbs.
func NewError(msg string) Falta {
panicIfStringHasVerbs(msg)
return Falta{
errFmt: msg,
error: fmt.Errorf(msg),
}
}
// Wrap wraps the error provided with the Falta instance.
func (f Falta) Wrap(err error) Falta {
f.error = fmt.Errorf("%s: %w", f.error.Error(), err)
f.wrappedErr = err
return f
}
// Annotate adds an annotation to the error to provide more context to why it's happening
//
// NOTE (a20r, 2024-02-25): It panics if the provided annotation contains any fmt verbs.
func (f Falta) Annotate(annotation string) Falta {
panicIfStringHasVerbs(annotation)
f.error = fmt.Errorf("%s: %s", f.error.Error(), annotation)
return f
}
// Unwrap returns the wrapped error if there is one.
func (f Falta) Unwrap() error {
return f.wrappedErr
}
// Is returns true if the error provided is a Falta instance created by the same factory.
func (f Falta) Is(err error) bool {
if f.wrappedErr != nil && errors.Is(err, f.wrappedErr) {
return true
}
other := Falta{}
errAs := errors.As(err, &other) && other.errFmt == f.errFmt
errFmtEq := err.Error() == f.errFmt
errValueEq := err.Error() == f.Error()
return errAs || errFmtEq || errValueEq
}
// Capture captures the error provided and wraps it with the Falta instance if it's not nil. This should be called
// with defer at the top of the function for which you are trying to capture the error. This ensures that all errors
// returned from your function will be wrapped by the function passed into Capture. You should use a named return
// value for the error so that the error Capture wraps is the one returned from teh function.
func (f Falta) Capture(err *error) {
if *err != nil {
*err = f.Wrap(*err)
}
}
var verbsRegex *regexp.Regexp
func init() {
const verbsRegexStr = `\%\w`
re, err := regexp.Compile(verbsRegexStr)
if err != nil {
panic(fmt.Errorf("falta: cannot compile verbs regex: %w", err))
}
verbsRegex = re
}
func panicIfStringHasVerbs(msg string) {
if verbsRegex.MatchString(msg) {
panic(fmt.Errorf(`falta: string "%s" has verbs`, msg))
}
}
// New creates a new Falta instance that construct errors by executing the provided template string on a struct
// of the type provided.
func New[T any](errFmt string) Factory[T] {
return newTmplFalta[T](errFmt)
}
// M is a convenience type for using Falta instances with maps.
type M map[string]any
// NewM returns a new ExtendableFactory instance using a template that expects a falta.M (i.e., map[string]any).
// This is a convenience function for calling falta.New[falta.M](...)
func NewM(errFmt string) ExtendableFactory[M] {
return newTmplFalta[M](errFmt)
}
// Newf creates a new Falta instance that will construct errors using the printf format string provided.
func Newf(errFmt string) ExtendableFactory[any] {
return newFmtFactory(errFmt)
}
type tmplFalta[T any] struct {
errFmt string
tmpl *template.Template
}
func newTmplFalta[T any](errFmt string) tmplFalta[T] {
return tmplFalta[T]{
errFmt: errFmt,
tmpl: template.Must(template.New("tmplFactoryFmt").Parse(errFmt)),
}
}
// New constructs a new error by executing the Falta's template with the struct provided. It panics if the template
// returns an error with it executes.
func (f tmplFalta[T]) New(vs ...T) Falta {
if len(vs) == 0 {
return Falta{errFmt: f.errFmt, error: f}
}
builder := new(strings.Builder)
if err := f.tmpl.Execute(builder, vs[0]); err != nil {
panic(fmt.Errorf("falta: cannot execute template: %w", err))
}
return Falta{errFmt: f.errFmt, error: fmt.Errorf(builder.String())}
}
func (f tmplFalta[T]) Extend(other Factory[T]) ExtendableFactory[T] {
v, ok := other.(tmplFalta[T])
if !ok {
panic(fmt.Errorf("falta: tmpl factories can only be extended by other tmpl factories with the same type"))
}
return newTmplFalta[T](f.errFmt + " " + v.errFmt)
}
func (f tmplFalta[T]) Error() string {
return f.errFmt
}
func (f tmplFalta[T]) Is(err error) bool {
other := Falta{}
errAs := errors.As(err, &other) && other.errFmt == f.errFmt
errFmtEq := err.Error() == f.errFmt
errValueEq := err.Error() == f.Error()
return errAs || errFmtEq || errValueEq
}
type fmtFalta struct {
errFmt string
}
func newFmtFactory(errFmt string) fmtFalta {
return fmtFalta{
errFmt: errFmt,
}
}
func (f fmtFalta) New(vs ...any) Falta {
if len(vs) == 0 {
return Falta{errFmt: f.errFmt, error: f}
}
return Falta{errFmt: f.errFmt, error: fmt.Errorf(f.errFmt, vs...)}
}
func (f fmtFalta) Extend(other Factory[any]) ExtendableFactory[any] {
v, ok := other.(fmtFalta)
if !ok {
panic(fmt.Errorf("falta: fmt factories can only be extended by other fmt factories"))
}
return newFmtFactory(f.errFmt + " " + v.errFmt)
}
func (f fmtFalta) Error() string {
return f.errFmt
}
func (f fmtFalta) Is(err error) bool {
other := Falta{}
errAs := errors.As(err, &other) && other.errFmt == f.errFmt
errFmtEq := err.Error() == f.errFmt
errValueEq := err.Error() == f.Error()
return errAs || errFmtEq || errValueEq
}