-
Notifications
You must be signed in to change notification settings - Fork 2
/
error_response.go
61 lines (51 loc) · 1.43 KB
/
error_response.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
package hermes
import "sync"
var (
InternalServerErrorCode = "internal-server-error"
InternalServerErrorMessage = "We encountered an internal error or misconfiguration and was unable to complete your request."
NotFoundErrorCode = "not-found"
NotFoundErrorMessage = "We could not find the resource you requested."
MethodNotAllowedErrorCode = "method-not-allowed"
MethodNotAllowedErrorMessage = "We believe that the used request method is inappropriate for the resource you requested."
)
var errorResponsePool = &sync.Pool{
New: func() interface{} {
return &errorResponse{
Data: make(map[string]interface{}),
}
},
}
type errorResponse struct {
defaultStatus int
Status int
Data map[string]interface{}
}
func acquireErrorResponse(status int) *errorResponse {
r := errorResponsePool.Get().(*errorResponse)
r.defaultStatus = status
r.Status = status
return r
}
func releaseErrorResponse(r *errorResponse) {
r.reset()
errorResponsePool.Put(r)
}
func (response *errorResponse) reset() {
response.defaultStatus = 0
response.Status = 0
for key := range response.Data {
delete(response.Data, key)
}
}
func (response *errorResponse) SetParam(name string, value interface{}) {
switch name {
case "statusCode":
if v, ok := value.(int); ok && response.Status == response.defaultStatus {
response.Status = v
}
default:
if _, found := response.Data[name]; !found {
response.Data[name] = value
}
}
}