-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
99 lines (87 loc) · 2.75 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 httperrors
import (
"encoding/json"
"fmt"
"net/http"
)
const (
invalidInput string = "Bad Request"
unauthorized string = "Unauthorized"
forbidden string = "Forbidden"
notFound string = "Not Found"
methodNotAllowed string = "Method Not Allowed"
entityTooLarge string = "Entity Too Large"
unprocessableEntity string = "Unprocessable Entity"
failedDependency string = "Failed Dependency"
tooManyRequests string = "Too Many Requests"
internal string = "Internal Server Error"
badGateway string = "Bad Gateway"
serviceUnavailable string = "Service Unavailable"
)
var errorCodeMap = map[int]string{
http.StatusBadRequest: invalidInput,
http.StatusUnauthorized: unauthorized,
http.StatusForbidden: forbidden,
http.StatusNotFound: notFound,
http.StatusMethodNotAllowed: methodNotAllowed,
http.StatusRequestEntityTooLarge: entityTooLarge,
http.StatusUnprocessableEntity: unprocessableEntity,
http.StatusFailedDependency: failedDependency,
http.StatusTooManyRequests: tooManyRequests,
http.StatusInternalServerError: internal,
http.StatusBadGateway: badGateway,
http.StatusServiceUnavailable: serviceUnavailable,
}
var marshalJSON = json.Marshal
// ErrorJSON is the response format for the response returned
// from WriteError.
type ErrorJSON struct {
Code int `json:"code"`
Message string `json:"message"`
Reason string `json:"reason"`
}
// WriteError writes a JSON formatted error response via the given
// http.ResponseWriter given a code and reason for the error.
//
// Example: Given an http.StatusNotFound (404) as the errorCode and
// "Could not find user" as the reason, the response would be:
//
// {
// Code: 404,
// Message: "Not Found",
// Reason: "Could not find user"
// }
func WriteError(w http.ResponseWriter, errorCode int, reason string) {
_, exists := errorCodeMap[errorCode]
if !exists {
panic("Invalid error code.")
}
errorResponse := ErrorJSON{
Code: errorCode,
Message: errorCodeMap[errorCode],
Reason: reason,
}
response, err := marshalJSON(errorResponse)
if err != nil {
http.Error(w, reason, errorCode)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(errorCode)
_, _ = w.Write(response)
}
// ErrorList is an error implementation that combines multiple error
// implementations into one.
type ErrorList struct {
Errors []error
}
// New returns a new ErrorList from the given errors.
func New(errs []error) ErrorList {
return ErrorList{
Errors: errs,
}
}
// Error returns the error messages of all individual errors contained in the
// list.
func (err ErrorList) Error() string {
return fmt.Sprintf("errors: %v", err.Errors)
}