-
Notifications
You must be signed in to change notification settings - Fork 18
/
error.go
217 lines (182 loc) · 5.46 KB
/
error.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
package errors
import "fmt"
const (
// ActualField is a key for actual value in error fields
ActualField = "Actual"
// ExpectedField is a key for expected value in error fields
ExpectedField = "Expected"
)
// AssertError is a common interface for all errors in the package
type AssertError interface {
error
WithNameError
WithFields
WithAttachments
WithTrace
}
// WithNameError is interface for creates allure step.
// If function returns error, which implement this interface, allure step will create automatically
type WithNameError interface {
GetName() string
SetName(string)
}
// WithFields is interface for put parameters in allure step.
// If function returns error, which implement this interface, parameters will add to allure step
type WithFields interface {
GetFields() map[string]interface{}
PutFields(map[string]interface{})
}
// WithTrace is interface for put trace in logs
type WithTrace interface {
GetTrace() string
SetTrace(string)
}
// Attachment represents an attachment to Allure with properties like name, MIME type, and content.
type Attachment struct {
Name string // Name of the attachment.
MimeType string // MIME type of the attachment.
Content []byte // Content of the attachment.
}
// WithAttachments is an interface that defines methods for managing attachments.
type WithAttachments interface {
GetAttachments() []*Attachment
PutAttachment(a *Attachment)
}
// CuteError is a struct for error with additional fields for allure and logs
type CuteError struct {
// Optional is a flag to determine if the error is optional
// If the error is optional, it will not fail the test
Optional bool
// Require is a flag to determine if the error is required
// If the error is required, it will fail the test
Require bool
// Broken is a flag to determine if the error is broken
// If the error is broken, it will fail the test and mark the test as broken in allure
Broken bool
// Name is a name of the error
Name string
// Message is a message of the error
Message string
// Err is a wrapped error
Err error
// Trace is a trace of the error
// It could be a file path, function name, or any other information
Trace string
// Fields is a map of additional fields for the error
// It could be actual and expected values, parameters, or any other information
// ActualField and ExpectedField fields will be logged
Fields map[string]interface{}
// Attachments is a slice of attachments for the error
Attachments []*Attachment
}
// NewCuteError is the function, which creates cute error with "Name" and "Message" for allure
func NewCuteError(name string, err error) *CuteError {
return &CuteError{
Name: name,
Err: err,
}
}
// NewAssertError is the function, which creates error with "Actual" and "Expected" for allure
func NewAssertError(name string, message string, actual interface{}, expected interface{}) error {
return &CuteError{
Name: name,
Message: message,
Fields: map[string]interface{}{
ActualField: actual,
ExpectedField: expected,
},
}
}
// NewAssertErrorWithMessage is the function, which creates error with "Name" and "Message" for allure
// Deprecated: use NewEmptyAssertError instead
func NewAssertErrorWithMessage(name string, message string) error {
return NewEmptyAssertError(name, message)
}
// NewEmptyAssertError is the function, which creates error with "Name" and "Message" for allure
// Returns AssertError with empty fields
// You can use PutFields and PutAttachment to add additional information
// You can use SetOptional, SetRequire, SetBroken to change error behavior
func NewEmptyAssertError(name string, message string) AssertError {
return &CuteError{
Name: name,
Message: message,
Fields: map[string]interface{}{},
}
}
// Unwrap is a method to get wrapped error
// It is used for errors.Is and errors.As functions
func (a *CuteError) Unwrap() error {
return a.Err
}
// Error is a method to get error message
// It is used for fmt.Errorf and fmt.Println functions
func (a *CuteError) Error() string {
if a.Trace == "" {
return a.Message
}
errText := a.Message
if a.Err != nil {
errText = a.Err.Error()
}
return fmt.Sprintf("%s\nCalled from: %s", errText, a.Trace)
}
// GetName is a method to get error name
// It is used for allure step name
func (a *CuteError) GetName() string {
return a.Name
}
// SetName is a method to set error name
// It is used for allure step name
func (a *CuteError) SetName(name string) {
a.Name = name
}
// GetFields ...
func (a *CuteError) GetFields() map[string]interface{} {
return a.Fields
}
// PutFields ...
func (a *CuteError) PutFields(fields map[string]interface{}) {
for k, v := range fields {
a.Fields[k] = v
}
}
// GetAttachments ...
func (a *CuteError) GetAttachments() []*Attachment {
return a.Attachments
}
// PutAttachment ...
func (a *CuteError) PutAttachment(attachment *Attachment) {
a.Attachments = append(a.Attachments, attachment)
}
// IsOptional ...
func (a *CuteError) IsOptional() bool {
return a.Optional
}
// SetOptional ...
func (a *CuteError) SetOptional(opt bool) {
a.Optional = opt
}
// IsRequire ...
func (a *CuteError) IsRequire() bool {
return a.Require
}
// SetRequire ...
func (a *CuteError) SetRequire(b bool) {
a.Require = b
}
// IsBroken ...
func (a *CuteError) IsBroken() bool {
return a.Broken
}
// SetBroken ...
func (a *CuteError) SetBroken(b bool) {
a.Broken = b
}
// GetTrace ...
func (a *CuteError) GetTrace() string {
return a.Trace
}
// SetTrace ...
func (a *CuteError) SetTrace(trace string) {
a.Trace = trace
}