From 792023bb0e8bf049adcbb463e551a4d4d8e27c65 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Tue, 6 Sep 2016 14:36:09 +0100 Subject: [PATCH] Improve db error messages --- web/err.go | 26 ++++++++++++++++++++++---- web/routes.go | 7 +------ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/web/err.go b/web/err.go index 9e365d9e..f55283c5 100644 --- a/web/err.go +++ b/web/err.go @@ -16,12 +16,26 @@ package web import ( "github.com/abcum/fibre" + "github.com/abcum/surreal/kvs" ) func errors(err error, c *fibre.Context) { code := 500 - text := "" + text := err.Error() + + switch err.(type) { + default: + err = fibre.NewHTTPError(400, text) + case *kvs.DBError: + err = fibre.NewHTTPError(503, text) + case *kvs.TXError: + err = fibre.NewHTTPError(500, text) + case *kvs.KVError: + err = fibre.NewHTTPError(409, text) + case *kvs.CKError: + err = fibre.NewHTTPError(403, text) + } if e, ok := err.(*fibre.HTTPError); ok { code = e.Code() @@ -32,14 +46,18 @@ func errors(err error, c *fibre.Context) { default: c.Text(code, text) case "application/json": - c.JSON(code, errs[code]) + info := errs[code] + info["information"] = text + c.JSON(code, info) case "application/msgpack": - c.PACK(code, errs[code]) + info := errs[code] + info["information"] = text + c.PACK(code, info) } } -var errs = map[int]interface{}{ +var errs = map[int]map[string]interface{}{ 200: map[string]interface{}{ "code": 200, diff --git a/web/routes.go b/web/routes.go index 76cb84fa..39a20377 100644 --- a/web/routes.go +++ b/web/routes.go @@ -77,12 +77,7 @@ func routes(s *fibre.Fibre) { s.Post("/sql", func(c *fibre.Context) error { res, err := db.Execute(c, c.Request().Body) if err != nil { - return c.Send(500, map[string]interface{}{ - "code": 500, - "details": "Request problems detected", - "documentation": "https://docs.surreal.io/", - "information": err.Error(), - }) + return err } return c.Send(200, res) })