-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuilder.go
37 lines (32 loc) · 970 Bytes
/
builder.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
package goerr
// Builder keeps a set of key-value pairs and can create a new error and wrap error with the key-value pairs.
type Builder struct {
options []Option
}
// NewBuilder creates a new Builder
func NewBuilder(options ...Option) *Builder {
return &Builder{
options: options,
}
}
// With copies the current Builder and adds a new key-value pair.
func (x *Builder) With(options ...Option) *Builder {
newBuilder := &Builder{
options: x.options[:],
}
newBuilder.options = append(newBuilder.options, options...)
return newBuilder
}
// New creates a new error with message
func (x *Builder) New(msg string, options ...Option) *Error {
err := newError(append(x.options, options...)...)
err.msg = msg
return err
}
// Wrap creates a new Error with caused error and add message.
func (x *Builder) Wrap(cause error, msg string, options ...Option) *Error {
err := newError(append(x.options, options...)...)
err.msg = msg
err.cause = cause
return err
}