diff --git a/agent/http.go b/agent/http.go index e639d7d7b6..b9791232dd 100644 --- a/agent/http.go +++ b/agent/http.go @@ -31,6 +31,15 @@ func (e MethodNotAllowedError) Error() string { return fmt.Sprintf("method %s not allowed", e.Method) } +// BadRequestError should be returned by a handler when parameters or the payload are not valid +type BadRequestError struct { + Reason string +} + +func (e BadRequestError) Error() string { + return fmt.Sprintf("Bad request: %s", e.Reason) +} + // HTTPServer provides an HTTP api for an agent. type HTTPServer struct { *http.Server @@ -249,6 +258,11 @@ func (s *HTTPServer) wrap(handler endpoint, methods []string) http.HandlerFunc { return ok } + isBadRequest := func(err error) bool { + _, ok := err.(BadRequestError) + return ok + } + addAllowHeader := func(methods []string) { resp.Header().Add("Allow", strings.Join(methods, ",")) } @@ -269,6 +283,9 @@ func (s *HTTPServer) wrap(handler endpoint, methods []string) http.HandlerFunc { addAllowHeader(err.(MethodNotAllowedError).Allow) resp.WriteHeader(http.StatusMethodNotAllowed) // 405 fmt.Fprint(resp, err.Error()) + case isBadRequest(err): + resp.WriteHeader(http.StatusBadRequest) + fmt.Fprint(resp, err.Error()) default: resp.WriteHeader(http.StatusInternalServerError) fmt.Fprint(resp, err.Error())