mirror of
https://github.com/prometheus/prometheus
synced 2025-03-11 07:59:57 +00:00
Return HTTP server error codes for execution errors
This commit is contained in:
parent
a5461e1ad7
commit
bf84faa010
@ -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,
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user