From 93670aa129bcb76b7f859f5a23509e3e921d4391 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Fri, 18 Jan 2013 01:54:26 +0100 Subject: [PATCH] Return API errors in JSON format. --- api/query.go | 7 ++++--- rules/ast/printer.go | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/api/query.go b/api/query.go index 77ceec65e2..b041232474 100644 --- a/api/query.go +++ b/api/query.go @@ -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) diff --git a/rules/ast/printer.go b/rules/ast/printer.go index ce070f4c41..17c17fcec9 100644 --- a/rules/ast/printer.go +++ b/rules/ast/printer.go @@ -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) }