Return API errors in JSON format.

This commit is contained in:
Julius Volz 2013-01-18 01:54:26 +01:00
parent a20bf35997
commit 93670aa129
2 changed files with 6 additions and 5 deletions

View file

@ -2,6 +2,7 @@ package api
import (
"code.google.com/p/gorest"
"errors"
"github.com/matttproud/prometheus/rules"
"github.com/matttproud/prometheus/rules/ast"
"sort"
@ -11,7 +12,7 @@ import (
func (serv MetricsService) Query(Expr string, Json string) (result string) {
exprNode, err := rules.LoadExprFromString(Expr)
if err != nil {
return err.Error()
return ast.ErrorToJSON(err)
}
timestamp := time.Now()
@ -32,10 +33,10 @@ func (serv MetricsService) Query(Expr string, Json string) (result string) {
func (serv MetricsService) QueryRange(Expr string, End int64, Range int64, Step int64) string {
exprNode, err := rules.LoadExprFromString(Expr)
if err != nil {
return err.Error()
return ast.ErrorToJSON(err)
}
if exprNode.Type() != ast.VECTOR {
return "Expression does not evaluate to vector type" // TODO return errors correctly everywhere
return ast.ErrorToJSON(errors.New("Expression does not evaluate to vector type"))
}
rb := serv.ResponseBuilder()
rb.SetContentType(gorest.Application_Json)

View file

@ -129,7 +129,7 @@ func (matrix Matrix) ToString() string {
return strings.Join(metricStrings, "\n")
}
func errorToJSON(err error) string {
func ErrorToJSON(err error) string {
errorStruct := struct {
Type string
Error string
@ -155,7 +155,7 @@ func TypedValueToJSON(data interface{}, typeStr string) string {
}
dataJSON, err := json.MarshalIndent(dataStruct, "", "\t")
if err != nil {
return errorToJSON(err)
return ErrorToJSON(err)
}
return string(dataJSON)
}