|
1 | 1 | package distributor
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "net/http"
|
5 | 6 |
|
6 | 7 | "github.com/prometheus/common/log"
|
@@ -59,6 +60,39 @@ func (d *Distributor) UserStatsHandler(w http.ResponseWriter, r *http.Request) {
|
59 | 60 | // ValidateExprHandler validates a PromQL expression.
|
60 | 61 | func (d *Distributor) ValidateExprHandler(w http.ResponseWriter, r *http.Request) {
|
61 | 62 | _, err := promql.ParseExpr(r.FormValue("expr"))
|
62 |
| - valid := (err == nil) |
63 |
| - util.WriteJSONResponse(w, map[string]bool{"valid": valid}) |
| 63 | + |
| 64 | + // We mimick the response format of Prometheus's official API here for |
| 65 | + // consistency, but unfortunately its private types (string consts etc.) |
| 66 | + // aren't reusable. |
| 67 | + if err == nil { |
| 68 | + util.WriteJSONResponse(w, map[string]string{ |
| 69 | + "status": "success", |
| 70 | + }) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + parseErr, ok := err.(*promql.ParseErr) |
| 75 | + if !ok { |
| 76 | + // This should always be a promql.ParseErr. |
| 77 | + http.Error(w, fmt.Sprintf("unexpected error returned from PromQL parser: %v", err), http.StatusInternalServerError) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + // If the parsing input was a single line, parseErr.Line is 0 |
| 82 | + // and the generated error string omits the line entirely. But we |
| 83 | + // want to report line numbers consistently, no matter how many |
| 84 | + // lines there are (starting at 1). |
| 85 | + if parseErr.Line == 0 { |
| 86 | + parseErr.Line = 1 |
| 87 | + } |
| 88 | + w.WriteHeader(http.StatusBadRequest) |
| 89 | + util.WriteJSONResponse(w, map[string]interface{}{ |
| 90 | + "status": "error", |
| 91 | + "errorType": "bad_data", |
| 92 | + "error": err.Error(), |
| 93 | + "location": map[string]int{ |
| 94 | + "line": parseErr.Line, |
| 95 | + "pos": parseErr.Pos, |
| 96 | + }, |
| 97 | + }) |
64 | 98 | }
|
0 commit comments