-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
46 lines (38 loc) · 795 Bytes
/
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
package gq
import (
"encoding/json"
)
type Error struct {
Key string `json:"key,omitempty"`
Message string `json:"message,omitempty"`
Errors []error `json:"errors,omitempty"`
}
func NewError(key string, message string) Error {
return Error{
Key: key,
Message: message,
Errors: []error{},
}
}
func NewEmptyError(key string) Error {
return Error{
Key: key,
Errors: []error{},
}
}
func (self Error) Add(err error) Error {
if err, ok := err.(Error); ok {
self.Errors = append(self.Errors, err)
return self
}
self.Errors = append(self.Errors, NewError("", err.Error()))
return self
}
func (self Error) Error() string {
b, _ := json.Marshal(self)
return string(b)
}
func (self Error) String() string {
b, _ := json.Marshal(self)
return string(b)
}