Skip to content

Commit 406648a

Browse files
committed
Include parse error position in validate_expr response
1 parent 9a19286 commit 406648a

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

distributor/http_server.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package distributor
22

33
import (
4+
"fmt"
45
"net/http"
56

67
"github.com/prometheus/common/log"
@@ -59,6 +60,39 @@ func (d *Distributor) UserStatsHandler(w http.ResponseWriter, r *http.Request) {
5960
// ValidateExprHandler validates a PromQL expression.
6061
func (d *Distributor) ValidateExprHandler(w http.ResponseWriter, r *http.Request) {
6162
_, 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+
})
6498
}

0 commit comments

Comments
 (0)