-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_errors.go
41 lines (36 loc) · 1.49 KB
/
api_errors.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
package main
// APIErrorCause - причина ошибки API
type APIErrorCause struct {
Target string `json:"target"`
Сause string `json:"cause"`
}
// APIError - ошибка API
type APIError struct {
Code int `json:"code"`
Сauses []APIErrorCause `json:"causes"`
Description string `json:"description"`
}
// ErrorCodeToString - описание ошибки
func ErrorCodeToString(code int) string {
switch code {
case 28: return "Not find all request params"
case 29: return "Request params not validate"
case 41: return "Error create data"
case 44: return "There are no results"
case 400: return "Bad request"
case 401: return "Not authorized"
case 409: return "Conflict"
case 404: return "Not Found"
case 500: return "Internal Server Error"
case 503: return "Service Unavailable"
default: return "Unknown"
}
}
// NewSimpleAPIError - создание простых ошибок
func NewSimpleAPIError(code int) APIError {
return APIError{ Code: code, Сauses: make([]APIErrorCause,0), Description: ErrorCodeToString(code)}
}
// NewAPIError - создание ошибок с причинами
func NewAPIError(code int, causes []APIErrorCause) APIError {
return APIError{ Code: code, Сauses: causes, Description: ErrorCodeToString(code)}
}