forked from stellar-deprecated/bridge-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
99 lines (88 loc) · 3.05 KB
/
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
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
package protocols
import (
"encoding/json"
"net/http"
)
var (
// InternalServerError is an error response
InternalServerError = &ErrorResponse{Code: "internal_server_error", Message: "Internal Server Error, please try again.", Status: http.StatusInternalServerError}
// InvalidParameterError is an error response
InvalidParameterError = &ErrorResponse{Code: "invalid_parameter", Message: "Invalid parameter.", Status: http.StatusBadRequest}
// MissingParameterError is an error response
MissingParameterError = &ErrorResponse{Code: "missing_parameter", Message: "Required parameter is missing.", Status: http.StatusBadRequest}
)
// NewInternalServerError creates and returns a new InternalServerError
func NewInternalServerError(logMessage string, logData map[string]interface{}) *ErrorResponse {
return &ErrorResponse{
Status: InternalServerError.Status,
Code: InternalServerError.Code,
Message: InternalServerError.Message,
LogMessage: logMessage,
LogData: logData,
}
}
// NewInvalidParameterError creates and returns a new InvalidParameterError
func NewInvalidParameterError(name, value, moreInfo string, additionalLogData ...map[string]interface{}) *ErrorResponse {
logData := map[string]interface{}{"name": name, "value": value}
if len(additionalLogData) == 1 {
for k, v := range additionalLogData[0] {
logData[k] = v
}
}
data := map[string]interface{}{}
if name != "" {
data["name"] = name
}
return &ErrorResponse{
Status: InvalidParameterError.Status,
Code: InvalidParameterError.Code,
Message: InvalidParameterError.Message,
MoreInfo: moreInfo,
Data: data,
LogData: logData,
}
}
// NewMissingParameter creates and returns a new MissingParameterError
func NewMissingParameter(name string) *ErrorResponse {
data := map[string]interface{}{"name": name}
return &ErrorResponse{
Status: MissingParameterError.Status,
Code: MissingParameterError.Code,
Message: MissingParameterError.Message,
Data: data,
LogData: data,
}
}
// ErrorResponse represents error response and implements server.Response and error interfaces
type ErrorResponse struct {
// HTTP status code
Status int `json:"-"`
// Error status code
Code string `json:"code"`
// Error message that will be returned to API consumer
Message string `json:"message"`
// Additional information returned to API consumer
MoreInfo string `json:"more_info,omitempty"`
// Error data that will be returned to API consumer
Data map[string]interface{} `json:"data,omitempty"`
// Error message that will be logged.
LogMessage string `json:"-"`
// Error data that will be logged.
LogData map[string]interface{} `json:"-"`
}
// Error returns Message or LogMessage if set
func (error *ErrorResponse) Error() string {
if error.LogMessage != "" {
return error.LogMessage
}
return error.Message
}
// HTTPStatus returns ErrorResponse.Status
func (error *ErrorResponse) HTTPStatus() int {
return error.Status
}
// Marshal marshals ErrorResponse
func (error *ErrorResponse) Marshal() []byte {
json, _ := json.MarshalIndent(error, "", " ")
return json
}