@@ -17,7 +17,6 @@ limitations under the License.
17
17
package api
18
18
19
19
import (
20
- "encoding/json"
21
20
"fmt"
22
21
"net/http"
23
22
"strconv"
@@ -29,23 +28,25 @@ type HTTPError struct {
29
28
}
30
29
31
30
type ErrorResponse struct {
32
- Code string `json:"code"`
33
- Message string `json:"message"`
31
+ Code string `json:"code"`
32
+ Message string `json:"message"`
33
+ ValidationErrors map [string ]string `json:"validation_errors,omitempty"`
34
34
}
35
35
36
36
type ErrorEnvelope struct {
37
37
Error * HTTPError `json:"error"`
38
38
}
39
39
40
+ // LogError logs an error message with the request details.
40
41
func (a * App ) LogError (r * http.Request , err error ) {
41
42
var (
42
43
method = r .Method
43
44
uri = r .URL .RequestURI ()
44
45
)
45
-
46
46
a .logger .Error (err .Error (), "method" , method , "uri" , uri )
47
47
}
48
48
49
+ // LogWarn logs a warning message with the request details.
49
50
func (a * App ) LogWarn (r * http.Request , message string ) {
50
51
var (
51
52
method = r .Method
@@ -55,18 +56,7 @@ func (a *App) LogWarn(r *http.Request, message string) {
55
56
a .logger .Warn (message , "method" , method , "uri" , uri )
56
57
}
57
58
58
- //nolint:unused
59
- func (a * App ) badRequestResponse (w http.ResponseWriter , r * http.Request , err error ) {
60
- httpError := & HTTPError {
61
- StatusCode : http .StatusBadRequest ,
62
- ErrorResponse : ErrorResponse {
63
- Code : strconv .Itoa (http .StatusBadRequest ),
64
- Message : err .Error (),
65
- },
66
- }
67
- a .errorResponse (w , r , httpError )
68
- }
69
-
59
+ // errorResponse writes an error response to the client.
70
60
func (a * App ) errorResponse (w http.ResponseWriter , r * http.Request , httpError * HTTPError ) {
71
61
env := ErrorEnvelope {Error : httpError }
72
62
@@ -77,6 +67,7 @@ func (a *App) errorResponse(w http.ResponseWriter, r *http.Request, httpError *H
77
67
}
78
68
}
79
69
70
+ // HTTP: 500
80
71
func (a * App ) serverErrorResponse (w http.ResponseWriter , r * http.Request , err error ) {
81
72
a .LogError (r , err )
82
73
@@ -90,28 +81,19 @@ func (a *App) serverErrorResponse(w http.ResponseWriter, r *http.Request, err er
90
81
a .errorResponse (w , r , httpError )
91
82
}
92
83
93
- func (a * App ) notFoundResponse (w http.ResponseWriter , r * http.Request ) {
94
- httpError := & HTTPError {
95
- StatusCode : http .StatusNotFound ,
96
- ErrorResponse : ErrorResponse {
97
- Code : strconv .Itoa (http .StatusNotFound ),
98
- Message : "the requested resource could not be found" ,
99
- },
100
- }
101
- a .errorResponse (w , r , httpError )
102
- }
103
-
104
- func (a * App ) methodNotAllowedResponse (w http.ResponseWriter , r * http.Request ) {
84
+ // HTTP: 400
85
+ func (a * App ) badRequestResponse (w http.ResponseWriter , r * http.Request , err error ) {
105
86
httpError := & HTTPError {
106
- StatusCode : http .StatusMethodNotAllowed ,
87
+ StatusCode : http .StatusBadRequest ,
107
88
ErrorResponse : ErrorResponse {
108
- Code : strconv .Itoa (http .StatusMethodNotAllowed ),
109
- Message : fmt . Sprintf ( "the %s method is not supported for this resource" , r . Method ),
89
+ Code : strconv .Itoa (http .StatusBadRequest ),
90
+ Message : err . Error ( ),
110
91
},
111
92
}
112
93
a .errorResponse (w , r , httpError )
113
94
}
114
95
96
+ // HTTP: 401
115
97
func (a * App ) unauthorizedResponse (w http.ResponseWriter , r * http.Request ) {
116
98
httpError := & HTTPError {
117
99
StatusCode : http .StatusUnauthorized ,
@@ -123,6 +105,7 @@ func (a *App) unauthorizedResponse(w http.ResponseWriter, r *http.Request) {
123
105
a .errorResponse (w , r , httpError )
124
106
}
125
107
108
+ // HTTP: 403
126
109
func (a * App ) forbiddenResponse (w http.ResponseWriter , r * http.Request , msg string ) {
127
110
a .LogWarn (r , msg )
128
111
@@ -136,18 +119,51 @@ func (a *App) forbiddenResponse(w http.ResponseWriter, r *http.Request, msg stri
136
119
a .errorResponse (w , r , httpError )
137
120
}
138
121
139
- //nolint:unused
140
- func (a * App ) failedValidationResponse (w http.ResponseWriter , r * http.Request , errors map [string ]string ) {
141
- message , err := json .Marshal (errors )
142
- if err != nil {
143
- message = []byte ("{}" )
122
+ // HTTP: 404
123
+ func (a * App ) notFoundResponse (w http.ResponseWriter , r * http.Request ) {
124
+ httpError := & HTTPError {
125
+ StatusCode : http .StatusNotFound ,
126
+ ErrorResponse : ErrorResponse {
127
+ Code : strconv .Itoa (http .StatusNotFound ),
128
+ Message : "the requested resource could not be found" ,
129
+ },
130
+ }
131
+ a .errorResponse (w , r , httpError )
132
+ }
133
+
134
+ // HTTP: 405
135
+ func (a * App ) methodNotAllowedResponse (w http.ResponseWriter , r * http.Request ) {
136
+ httpError := & HTTPError {
137
+ StatusCode : http .StatusMethodNotAllowed ,
138
+ ErrorResponse : ErrorResponse {
139
+ Code : strconv .Itoa (http .StatusMethodNotAllowed ),
140
+ Message : fmt .Sprintf ("the %s method is not supported for this resource" , r .Method ),
141
+ },
144
142
}
143
+ a .errorResponse (w , r , httpError )
144
+ }
145
+
146
+ // HTTP:415
147
+ func (a * App ) unsupportedMediaTypeResponse (w http.ResponseWriter , r * http.Request , err error ) {
148
+ httpError := & HTTPError {
149
+ StatusCode : http .StatusUnsupportedMediaType ,
150
+ ErrorResponse : ErrorResponse {
151
+ Code : strconv .Itoa (http .StatusUnsupportedMediaType ),
152
+ Message : err .Error (),
153
+ },
154
+ }
155
+ a .errorResponse (w , r , httpError )
156
+ }
145
157
158
+ // HTTP: 422
159
+ // validationErrors is a map of field reference to error message.
160
+ func (a * App ) failedValidationResponse (w http.ResponseWriter , r * http.Request , validationErrors map [string ]string ) {
146
161
httpError := & HTTPError {
147
162
StatusCode : http .StatusUnprocessableEntity ,
148
163
ErrorResponse : ErrorResponse {
149
- Code : strconv .Itoa (http .StatusUnprocessableEntity ),
150
- Message : string (message ),
164
+ Code : strconv .Itoa (http .StatusUnprocessableEntity ),
165
+ Message : "request body was not valid" ,
166
+ ValidationErrors : validationErrors ,
151
167
},
152
168
}
153
169
a .errorResponse (w , r , httpError )
0 commit comments