-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
81 lines (67 loc) · 1.41 KB
/
builder.go
File metadata and controls
81 lines (67 loc) · 1.41 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
package errors
import "time"
// Builder 错误构造器
type Builder struct {
// 错误码
code *ErrCode
// 错误详情
message string
// 原始错误
cause error
// 元数据信息
metadata map[string]any
// 是否启用快速模式,快速模式不会捕获堆栈信息
fastMode bool
}
func NewBuilder() *Builder {
return &Builder{
metadata: map[string]any{},
}
}
func (b *Builder) WithFastMode() *Builder {
b.fastMode = true
return b
}
func (b *Builder) WithCode(code *ErrCode) *Builder {
b.code = code
return b
}
func (b *Builder) WithMessage(message string) *Builder {
b.message = message
return b
}
func (b *Builder) WithCause(cause error) *Builder {
b.cause = cause
return b
}
func (b *Builder) WithMetadata(key string, val any) *Builder {
b.metadata[key] = val
return b
}
func (b *Builder) WithMetadataMap(metadata map[string]any) *Builder {
for k, v := range metadata {
b.metadata[k] = v
}
return b
}
func (b *Builder) Build() Error {
if b.code == nil {
b.code = ErrInternal
}
if b.message == "" {
b.message = b.code.Message
}
impl := acquireError()
impl.code = b.code.Code
impl.message = b.message
impl.httpStatus = b.code.HttpStatus
impl.errType = b.code.Type
impl.timestamp = time.Now().UTC()
impl.cause = b.cause
impl.metadata = b.metadata
// 不开启快速模式,则记录堆栈信息
if !b.fastMode {
impl.stackTrace = captureStackTrace(2)
}
return impl
}