Skip to content

Commit bffcfa7

Browse files
authored
Merge pull request #60 from cabify/colega/pass-correct-status-code-to-after-func
Pass the correct status code to AfterFunc
2 parents 97c3400 + dcfeecf commit bffcfa7

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

v2/server.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (s *Server) HasMethod(method string) bool {
146146
// ServeHTTP
147147
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
148148
if r.Method != "POST" {
149-
WriteError(w, 405, "rpc: POST method required, received "+r.Method)
149+
WriteError(w, http.StatusMethodNotAllowed, "rpc: POST method required, received "+r.Method)
150150
return
151151
}
152152
contentType := r.Header.Get("Content-Type")
@@ -162,26 +162,26 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
162162
codec = c
163163
}
164164
} else if codec = s.codecs[strings.ToLower(contentType)]; codec == nil {
165-
WriteError(w, 415, "rpc: unrecognized Content-Type: "+contentType)
165+
WriteError(w, http.StatusUnsupportedMediaType, "rpc: unrecognized Content-Type: "+contentType)
166166
return
167167
}
168168
// Create a new codec request.
169169
codecReq := codec.NewRequest(r)
170170
// Get service method to be called.
171171
method, errMethod := codecReq.Method()
172172
if errMethod != nil {
173-
codecReq.WriteError(w, 400, errMethod)
173+
codecReq.WriteError(w, http.StatusBadRequest, errMethod)
174174
return
175175
}
176176
serviceSpec, methodSpec, errGet := s.services.get(method)
177177
if errGet != nil {
178-
codecReq.WriteError(w, 400, errGet)
178+
codecReq.WriteError(w, http.StatusBadRequest, errGet)
179179
return
180180
}
181181
// Decode the args.
182182
args := reflect.New(methodSpec.argsType)
183183
if errRead := codecReq.ReadRequest(args.Interface()); errRead != nil {
184-
codecReq.WriteError(w, 400, errRead)
184+
codecReq.WriteError(w, http.StatusBadRequest, errRead)
185185
return
186186
}
187187

@@ -227,18 +227,22 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
227227

228228
// Extract the result to error if needed.
229229
var errResult error
230-
if !errValue[0].IsNil() {
231-
errResult = errValue[0].Interface().(error)
230+
statusCode := http.StatusOK
231+
errInter := errValue[0].Interface()
232+
if errInter != nil {
233+
statusCode = http.StatusBadRequest
234+
errResult = errInter.(error)
232235
}
233236

234237
// Prevents Internet Explorer from MIME-sniffing a response away
235238
// from the declared content-type
236239
w.Header().Set("x-content-type-options", "nosniff")
240+
237241
// Encode the response.
238242
if errResult == nil {
239243
codecReq.WriteResponse(w, reply.Interface())
240244
} else {
241-
codecReq.WriteError(w, 400, errResult)
245+
codecReq.WriteError(w, statusCode, errResult)
242246
}
243247

244248
// Call the registered After Function
@@ -247,7 +251,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
247251
Request: r,
248252
Method: method,
249253
Error: errResult,
250-
StatusCode: 200,
254+
StatusCode: statusCode,
251255
})
252256
}
253257
}

0 commit comments

Comments
 (0)