Return HTTP server error codes for execution errors

This commit is contained in:
Tobias Schmidt 2015-11-11 14:00:54 -08:00
parent a5461e1ad7
commit bf84faa010
2 changed files with 15 additions and 3 deletions

View File

@ -255,7 +255,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

@ -373,8 +373,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)