-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
133 lines (106 loc) · 3.01 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
package http
import (
"fmt"
"net/http"
"strconv"
"encoding/json"
)
type (
BaseError interface {
Headers() map[string]string
Error() string
Response() interface{}
StatusCode() int
}
Error struct {
System string `json:"system,omitempty"`
Status int `json:"status,omitempty"`
Series int `json:"series,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
DeveloperMessage string `json:"developerMessage,omitempty"`
MoreInfo string `json:"moreInfo,omitempty"`
}
ErrorSystem struct {
System string `json:"system,omitempty"`
Series int `json:"series,omitempty"`
}
)
func (e Error) Headers() map[string]string {
return nil
}
func (Error) SetHeader(key, value string) {}
func (Error) GetHeader(key string) string {
return ""
}
func (e Error) Response() interface{} {
return e
}
func (e Error) Error() string {
return fmt.Sprintf("system: %s; status: %d; code: %s; message: %s; moreInfo: %s", e.System, e.Status, e.Code, e.Message, e.MoreInfo)
}
func (e Error) StatusCode() int {
return e.Status
}
func NewErrorSystem(system string, series int) *ErrorSystem {
return &ErrorSystem{
System: system,
Series: series,
}
}
func (errSys *ErrorSystem) NewError(status, code int, messages ...string) *Error {
var message, devMessage string
switch len(messages) {
case 2:
message, devMessage = messages[0], messages[1]
case 1:
message = messages[0]
}
c := errSys.System + "." + strconv.Itoa(status) + strconv.Itoa(errSys.Series) + strconv.Itoa(code)
return &Error{
System: errSys.System,
Status: status,
Series: errSys.Series,
Code: c,
Message: message,
DeveloperMessage: devMessage,
MoreInfo: "docs/" + c,
}
}
func (errSys *ErrorSystem) FromError(err BaseError) BaseError {
derr, ok := err.(Error)
if ok {
c, _ := strconv.Atoi(derr.Code)
return errSys.NewError(err.StatusCode(), c, derr.Message, derr.DeveloperMessage)
}
derr1, ok := err.(*Error)
if ok {
c, _ := strconv.Atoi(derr1.Code)
return errSys.NewError(err.StatusCode(), c, derr1.Message, derr.DeveloperMessage)
}
return nil
}
func (errSys *ErrorSystem) BadRequest(code int, messages ...string) *Error {
return errSys.NewError(http.StatusBadRequest, code, messages...)
}
func (errSys *ErrorSystem) InternalServerError(code int, messages ...string) *Error {
return errSys.NewError(http.StatusInternalServerError, code, messages...)
}
func (errSys *ErrorSystem) NotFound(code int, messages ...string) *Error {
return errSys.NewError(http.StatusNotFound, code, messages...)
}
func (errSys *ErrorSystem) Forbidden(code int, messages ...string) *Error {
return errSys.NewError(http.StatusForbidden, code, messages...)
}
func IfJsonError(data []byte) *Error {
er := &Error{}
err := json.Unmarshal(data, er)
if err != nil {
return nil
}
isErr := er.Series != 0 && er.Message != "" && er.Code != "" && er.System != ""
if !isErr {
return nil
}
return er
}