-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
74 lines (60 loc) · 1.54 KB
/
error.go
File metadata and controls
74 lines (60 loc) · 1.54 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
package errorx
import (
"fmt"
)
type Error interface {
Error() string // error interface
String() string // Stringer interface
Code() int
Message() string
Data() interface{}
}
type ErrorPayload struct {
CodePayload int `json:"code"`
MessagePayload string `json:"message,omitempty"`
DataPlayload interface{} `json:"data,omitempty"`
}
func New(code int, message string) Error {
return ErrorPayload{
CodePayload: code,
MessagePayload: message,
}
}
func NewWithData(code int, message string, data interface{}) Error {
return ErrorPayload{
CodePayload: code,
MessagePayload: message,
DataPlayload: data,
}
}
func NewFromErr(code int, err error) Error {
return ErrorPayload{
CodePayload: code,
MessagePayload: err.Error(),
}
}
func NewErrorPayloadFromError(errx Error) *ErrorPayload {
return &ErrorPayload{
CodePayload: errx.Code(),
MessagePayload: errx.Message(),
DataPlayload: errx.Data(),
}
}
func (thisRef ErrorPayload) Code() int {
return thisRef.CodePayload
}
func (thisRef ErrorPayload) Message() string {
return thisRef.MessagePayload
}
func (thisRef ErrorPayload) Data() interface{} {
return thisRef.DataPlayload
}
func (thisRef ErrorPayload) String() string {
if thisRef.DataPlayload != nil {
return fmt.Sprintf("code: %d, message: %s, data: %v", thisRef.CodePayload, thisRef.MessagePayload, thisRef.DataPlayload)
}
return fmt.Sprintf("code: %d, message: %s", thisRef.CodePayload, thisRef.MessagePayload)
}
func (thisRef ErrorPayload) Error() string {
return thisRef.String()
}