-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
106 lines (94 loc) · 2.62 KB
/
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
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
package errors
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"net/http"
"sort"
)
// ErrorResponse is the response that represents an error.
type ErrorResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Details interface{} `json:"details,omitempty"`
}
// Error is required by the error interface.
func (e ErrorResponse) Error() string {
return e.Message
}
// StatusCode is required by routing.HTTPError interface.
func (e ErrorResponse) StatusCode() int {
return e.Status
}
// InternalServerError creates a new error response representing an internal server error (HTTP 500)
func InternalServerError(msg string) ErrorResponse {
if msg == "" {
msg = "We encountered an error while processing your request."
}
return ErrorResponse{
Status: http.StatusInternalServerError,
Message: msg,
}
}
// NotFound creates a new error response representing a resource-not-found error (HTTP 404)
func NotFound(msg string) ErrorResponse {
if msg == "" {
msg = "The requested resource was not found."
}
return ErrorResponse{
Status: http.StatusNotFound,
Message: msg,
}
}
// Unauthorized creates a new error response representing an authentication/authorization failure (HTTP 401)
func Unauthorized(msg string) ErrorResponse {
if msg == "" {
msg = "You are not authenticated to perform the requested action."
}
return ErrorResponse{
Status: http.StatusUnauthorized,
Message: msg,
}
}
// Forbidden creates a new error response representing an authorization failure (HTTP 403)
func Forbidden(msg string) ErrorResponse {
if msg == "" {
msg = "You are not authorized to perform the requested action."
}
return ErrorResponse{
Status: http.StatusForbidden,
Message: msg,
}
}
// BadRequest creates a new error response representing a bad request (HTTP 400)
func BadRequest(msg string) ErrorResponse {
if msg == "" {
msg = "Your request is in a bad format."
}
return ErrorResponse{
Status: http.StatusBadRequest,
Message: msg,
}
}
type invalidField struct {
Field string `json:"field"`
Error string `json:"error"`
}
// InvalidInput creates a new error response representing a data validation error (HTTP 400).
func InvalidInput(errs validation.Errors) ErrorResponse {
var details []invalidField
var fields []string
for field := range errs {
fields = append(fields, field)
}
sort.Strings(fields)
for _, field := range fields {
details = append(details, invalidField{
Field: field,
Error: errs[field].Error(),
})
}
return ErrorResponse{
Status: http.StatusBadRequest,
Message: "There is some problem with the data you submitted.",
Details: details,
}
}