Merge pull request #1213 from prometheus/fix-wrong-http-status-codes

Return HTTP server error codes for execution errors
This commit is contained in:
Tobias Schmidt 2015-11-12 09:12:17 -08:00
commit 7a6a0630d1
2 changed files with 15 additions and 3 deletions

View file

@ -269,7 +269,19 @@ func respond(w http.ResponseWriter, data interface{}) {
func respondError(w http.ResponseWriter, apiErr *apiError, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(422)
var code int
switch apiErr.typ {
case errorBadData:
code = http.StatusBadRequest
case errorExec:
code = 422
case errorCanceled, errorTimeout:
code = http.StatusServiceUnavailable
default:
code = http.StatusInternalServerError
}
w.WriteHeader(code)
b, err := json.Marshal(&response{
Status: statusError,

View file

@ -380,8 +380,8 @@ func TestRespondError(t *testing.T) {
t.Fatalf("Error reading response body: %s", err)
}
if resp.StatusCode != 422 {
t.Fatalf("Return code %d expected in error response but got %d", 422, resp.StatusCode)
if want, have := http.StatusServiceUnavailable, resp.StatusCode; want != have {
t.Fatalf("Return code %d expected in error response but got %d", want, have)
}
if h := resp.Header.Get("Content-Type"); h != "application/json" {
t.Fatalf("Expected Content-Type %q but got %q", "application/json", h)