mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-02 08:31:11 -08:00
Merge pull request #1213 from prometheus/fix-wrong-http-status-codes
Return HTTP server error codes for execution errors
This commit is contained in:
commit
7a6a0630d1
|
@ -269,7 +269,19 @@ func respond(w http.ResponseWriter, data interface{}) {
|
||||||
|
|
||||||
func respondError(w http.ResponseWriter, apiErr *apiError, data interface{}) {
|
func respondError(w http.ResponseWriter, apiErr *apiError, data interface{}) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
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{
|
b, err := json.Marshal(&response{
|
||||||
Status: statusError,
|
Status: statusError,
|
||||||
|
|
|
@ -380,8 +380,8 @@ func TestRespondError(t *testing.T) {
|
||||||
t.Fatalf("Error reading response body: %s", err)
|
t.Fatalf("Error reading response body: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 422 {
|
if want, have := http.StatusServiceUnavailable, resp.StatusCode; want != have {
|
||||||
t.Fatalf("Return code %d expected in error response but got %d", 422, resp.StatusCode)
|
t.Fatalf("Return code %d expected in error response but got %d", want, have)
|
||||||
}
|
}
|
||||||
if h := resp.Header.Get("Content-Type"); h != "application/json" {
|
if h := resp.Header.Get("Content-Type"); h != "application/json" {
|
||||||
t.Fatalf("Expected Content-Type %q but got %q", "application/json", h)
|
t.Fatalf("Expected Content-Type %q but got %q", "application/json", h)
|
||||||
|
|
Loading…
Reference in a new issue