-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_builder.go
More file actions
195 lines (165 loc) · 6.15 KB
/
error_builder.go
File metadata and controls
195 lines (165 loc) · 6.15 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
193
194
195
package throw
import (
"errors"
"fmt"
"github.com/pauloRohling/throw/attributes"
)
// ErrorBuilder is responsible for building [Error] objects.
//
// It has methods for adding attributes to the error, which can be used to provide more context
// about the error.
//
// It also has methods for setting the error message and returning the built error, which should
// be used at the end of the chain of methods.
type ErrorBuilder struct {
err *Error
}
// NewErrorBuilder creates a new [ErrorBuilder] without error type.
func NewErrorBuilder() *ErrorBuilder {
return &ErrorBuilder{
err: newError(""),
}
}
// NewTypedErrorBuilder creates a new [ErrorBuilder] with the given error type.
func NewTypedErrorBuilder(errType string) *ErrorBuilder {
return &ErrorBuilder{
err: newError(errType),
}
}
// Err sets the error and returns the builder for chaining. If the error is a [throw.Error], it will
// add all its attributes to the current error.
func (builder *ErrorBuilder) Err(err error) *ErrorBuilder {
if err == nil {
return builder
}
builder.err.err = err
builder.err.message = err.Error()
var throwError *Error
if !errors.As(err, &throwError) {
return builder
}
for key, attr := range throwError.attributes {
if builder.err.attributes[key] == nil {
builder.err.attributes[key] = attr
}
}
return builder
}
// PlainErr sets the error and returns the builder for chaining.
func (builder *ErrorBuilder) PlainErr(err error) *ErrorBuilder {
if err != nil {
builder.err.err = err
builder.err.message = err.Error()
}
return builder
}
// Str adds a [attributes.String] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Str(key, value string) *ErrorBuilder {
attr := attributes.NewString(key, value)
builder.err.add(attr)
return builder
}
// Any adds a [attributes.Any] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Any(key string, value any) *ErrorBuilder {
attr := attributes.NewAny(key, value)
builder.err.add(attr)
return builder
}
// Json adds a [attributes.Json] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Json(key string, value any) *ErrorBuilder {
attr := attributes.NewJson(key, value)
builder.err.add(attr)
return builder
}
// Uint adds a [attributes.Uint] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Uint(key string, value uint) *ErrorBuilder {
attr := attributes.NewUint(key, value)
builder.err.add(attr)
return builder
}
// Uint8 adds a [attributes.Uint8] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Uint8(key string, value uint8) *ErrorBuilder {
attr := attributes.NewUint8(key, value)
builder.err.add(attr)
return builder
}
// Uint16 adds a [attributes.Uint16] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Uint16(key string, value uint16) *ErrorBuilder {
attr := attributes.NewUint16(key, value)
builder.err.add(attr)
return builder
}
// Uint32 adds a [attributes.Uint32] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Uint32(key string, value uint32) *ErrorBuilder {
attr := attributes.NewUint32(key, value)
builder.err.add(attr)
return builder
}
// Uint64 adds a [attributes.Uint64] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Uint64(key string, value uint64) *ErrorBuilder {
attr := attributes.NewUint64(key, value)
builder.err.add(attr)
return builder
}
// Int adds a [attributes.Int] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Int(key string, value int) *ErrorBuilder {
attr := attributes.NewInt(key, value)
builder.err.add(attr)
return builder
}
// Int8 adds a [attributes.Int8] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Int8(key string, value int8) *ErrorBuilder {
attr := attributes.NewInt8(key, value)
builder.err.add(attr)
return builder
}
// Int16 adds a [attributes.Int16] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Int16(key string, value int16) *ErrorBuilder {
attr := attributes.NewInt16(key, value)
builder.err.add(attr)
return builder
}
// Int32 adds a [attributes.Int32] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Int32(key string, value int32) *ErrorBuilder {
attr := attributes.NewInt32(key, value)
builder.err.add(attr)
return builder
}
// Int64 adds a [attributes.Int64] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Int64(key string, value int64) *ErrorBuilder {
attr := attributes.NewInt64(key, value)
builder.err.add(attr)
return builder
}
// Float32 adds a [attributes.Float32] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Float32(key string, value float32) *ErrorBuilder {
attr := attributes.NewFloat32(key, value)
builder.err.add(attr)
return builder
}
// Float64 adds a [attributes.Float64] attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Float64(key string, value float64) *ErrorBuilder {
attr := attributes.NewFloat64(key, value)
builder.err.add(attr)
return builder
}
// Attribute adds an attribute to the error and returns the builder for chaining.
func (builder *ErrorBuilder) Attribute(attribute Attribute) *ErrorBuilder {
builder.err.add(attribute)
return builder
}
// Attr adds an attribute to the error and returns the builder for chaining. It is a shorthand for [ErrorBuilder.Attribute].
func (builder *ErrorBuilder) Attr(attribute Attribute) *ErrorBuilder {
builder.err.add(attribute)
return builder
}
// Msg sets the error message and returns the error. It ends the chain of methods.
func (builder *ErrorBuilder) Msg(message string) *Error {
builder.err.message = message
return builder.err
}
// Msgf sets the error message with a formatted string and returns the error. It ends the chain of methods.
func (builder *ErrorBuilder) Msgf(message string, values ...any) *Error {
builder.err.message = fmt.Sprintf(message, values...)
return builder.err
}