mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2026-08-23 03:39:17 +03:00
Fixes https://github.com/VictoriaMetrics/VictoriaMetrics/issues/11330 Align with https://prometheus.io/docs/prometheus/latest/querying/api/#format-overview, return `400` instead of `422` when request parameters are missing or incorrect and should be fixed on the caller side. One exception is when the request hits a [resource limit](https://docs.victoriametrics.com/victoriametrics/cluster-victoriametrics/#resource-usage-limits), such as `search.maxPointsPerTimeseries`; it could be fixed by the caller(reducing the request range) or the admin(increasing the limit), but it is not caused by incorrect parameters, so in this case, still return 422. --------- Co-authored-by: Max Kotliar <mkotlyar@victoriametrics.com>
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
)
|
|
|
|
// InvalidParamError sets HTTP status code to 400 Bad Request for Prometheus querying APIs when parameters are missing or incorrect,
|
|
// see https://prometheus.io/docs/prometheus/latest/querying/api/#format-overview.
|
|
func InvalidParamError(err error) *ErrorWithStatusCode {
|
|
return &ErrorWithStatusCode{
|
|
Err: err,
|
|
StatusCode: http.StatusBadRequest,
|
|
}
|
|
}
|
|
|
|
// SendPrometheusError sends err to w in Prometheus querying API response format,
|
|
// and sets HTTP status code to 422 Unprocessable Entity when code is not set,
|
|
// see https://prometheus.io/docs/prometheus/latest/querying/api/#format-overview for more details.
|
|
func SendPrometheusError(w http.ResponseWriter, r *http.Request, err error) {
|
|
errStr := err.Error()
|
|
logHTTPError(r, errStr)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
statusCode := http.StatusUnprocessableEntity
|
|
var esc *ErrorWithStatusCode
|
|
if errors.As(err, &esc) {
|
|
statusCode = esc.StatusCode
|
|
}
|
|
w.WriteHeader(statusCode)
|
|
|
|
var ure *UserReadableError
|
|
if errors.As(err, &ure) {
|
|
err = ure
|
|
}
|
|
WritePrometheusErrorResponse(w, statusCode, err)
|
|
}
|
|
|
|
// UserReadableError is a type of error which supposed to be returned to the user without additional context.
|
|
type UserReadableError struct {
|
|
// Err is the error which needs to be returned to the user.
|
|
Err error
|
|
}
|
|
|
|
// Unwrap returns ure.Err.
|
|
//
|
|
// This is used by standard errors package. See https://golang.org/pkg/errors
|
|
func (ure *UserReadableError) Unwrap() error {
|
|
return ure.Err
|
|
}
|
|
|
|
// Error satisfies Error interface
|
|
func (ure *UserReadableError) Error() string {
|
|
return ure.Err.Error()
|
|
}
|