-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.go
More file actions
189 lines (164 loc) · 5.75 KB
/
Copy patherror.go
File metadata and controls
189 lines (164 loc) · 5.75 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
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
package helios
import "net/http"
// Error is interface of error that can be thrown
// for response
type Error interface {
GetMessage() map[string]interface{}
GetStatusCode() int
}
// ErrorAPI is standardized error of Charon app
type ErrorAPI struct {
StatusCode int
Code string
Message string
}
// GetMessage returns the message to shown as response body
func (apiError ErrorAPI) GetMessage() map[string]interface{} {
message := make(map[string]interface{})
message["code"] = apiError.Code
message["message"] = apiError.Message
return message
}
// GetStatusCode returns the http status code
func (apiError ErrorAPI) GetStatusCode() int {
return apiError.StatusCode
}
// ErrorForm is common error, usually after parsing the request body
type ErrorForm struct {
Code string
FieldError ErrorFormFieldNested
NonFieldError ErrorFormFieldAtomic
}
// NewErrorForm returns new empty ErrorForm
func NewErrorForm() ErrorForm {
return ErrorForm{
Code: "form_error",
FieldError: make(ErrorFormFieldNested),
NonFieldError: make(ErrorFormFieldAtomic, 0),
}
}
// ErrorFormField is the interface of error for a field in a form. A field
// in a form can be:
// - an atomic field (ex: username, fullname), using ErrorFormFieldAtomic
// - an array field (ex: todos, checkboxes), using ErrorFormFieldArray
// - a nested field (ex: address consist of zip code, city), using ErrorFormFieldNested
type ErrorFormField interface {
GetMessage() interface{}
IsError() bool
}
// ErrorFormFieldAtomic is error representation of one field, example:
// passwordErr := ErrorFormFieldAtomic{"password is too short". "password must include symbol"}
// // will be converted to json:
// ["password is too short","password must include symbol"]
type ErrorFormFieldAtomic []string
// GetMessage returns the json-friendly array copy of the error
func (err ErrorFormFieldAtomic) GetMessage() interface{} {
message := make([]string, 0)
for _, e := range err {
message = append(message, e)
}
return message
}
// IsError returns true if there is any error
func (err ErrorFormFieldAtomic) IsError() bool {
return len(err) > 0
}
// ErrorFormFieldArray is error representation of array field, example:
// todosErr := ErrorFormFieldArray{ErrorFormFieldAtomic{"todo can't be empty"}, ErrorFormFieldAtomic{}, ErrorFormFieldAtomic{"todo is duplicate"}}
// // will be converted to json:
// [["todo can't be empty"],[],["todo is duplicate"]]
type ErrorFormFieldArray []ErrorFormField
// GetMessage returns json-friendly array copy of the error
func (err ErrorFormFieldArray) GetMessage() interface{} {
message := make([]interface{}, 0)
for _, e := range err {
message = append(message, e.GetMessage())
}
return message
}
// IsError returns true if there is at least one member with err.
// Tt will iterate all the member.
func (err ErrorFormFieldArray) IsError() bool {
var isError bool = false
for _, e := range err {
if e.IsError() {
isError = true
}
}
return isError
}
// ErrorFormFieldNested is error representation other field error mapped by field name, example:
// addressErr := ErrorFormFieldNested{
// "zipCode": ErrorFormFieldAtomic{"zip code is invalid"},
// "city": ErrorFormFieldAtomic{"city can't be empty"},
// }
// // will be converted to json:
// {"city":["city can't be empty"],"zipCode":["zip code is invalid"]}
type ErrorFormFieldNested map[string]ErrorFormField
// GetMessage returns json-friendly map copy of the error
func (err ErrorFormFieldNested) GetMessage() interface{} {
message := make(map[string]interface{})
for k, v := range err {
message[k] = v.GetMessage()
}
return message
}
// IsError returns true if there is at least one member with err.
// It will iterate all the member.
func (err ErrorFormFieldNested) IsError() bool {
var isError bool = false
for _, v := range err {
if v.IsError() {
isError = true
}
}
return isError
}
// GetMessage returns the message to shown as response body
// it will include code (unique identifier) and map as message
// the message will contain field name as key and error as value
func (formError ErrorForm) GetMessage() map[string]interface{} {
messageFields := make(map[string]interface{})
for k, v := range formError.FieldError {
messageFields[k] = v.GetMessage()
}
messageFields["_error"] = formError.NonFieldError.GetMessage()
message := make(map[string]interface{})
if formError.Code == "" {
message["code"] = "form_error"
} else {
message["code"] = formError.Code
}
message["message"] = messageFields
return message
}
// IsError returns true if there is at least one error,
// and return false if there is no error on the struct
func (formError ErrorForm) IsError() bool {
return formError.FieldError.IsError() || formError.NonFieldError.IsError()
}
// GetStatusCode returns HTTP 400 Bad Request code
func (formError ErrorForm) GetStatusCode() int {
return http.StatusBadRequest
}
// ErrInternalServerError is general error that will be send
// if there is unexpected error on the server
var ErrInternalServerError = ErrorAPI{
StatusCode: http.StatusInternalServerError,
Code: "internal_server_error",
Message: "Error occured while processing the request",
}
// ErrUnsupportedContentType returned when request has content-type
// header that is unsupported
var ErrUnsupportedContentType = ErrorAPI{
StatusCode: http.StatusUnsupportedMediaType,
Code: "unsupported_content_type",
Message: "Currently, we are accepting application/json only",
}
// ErrJSONParseFailed will be returned when calling req.DeserializeRequestData
// but with bad JSON. For example, int field that supplied with string
var ErrJSONParseFailed = ErrorAPI{
StatusCode: http.StatusBadRequest,
Code: "failed_to_parse_json",
Message: "Failed to parse json request",
}