Improve db error messages

This commit is contained in:
Tobie Morgan Hitchcock 2016-09-06 14:36:09 +01:00
parent 065b64c429
commit 792023bb0e
2 changed files with 23 additions and 10 deletions

View file

@ -16,12 +16,26 @@ package web
import ( import (
"github.com/abcum/fibre" "github.com/abcum/fibre"
"github.com/abcum/surreal/kvs"
) )
func errors(err error, c *fibre.Context) { func errors(err error, c *fibre.Context) {
code := 500 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 { if e, ok := err.(*fibre.HTTPError); ok {
code = e.Code() code = e.Code()
@ -32,14 +46,18 @@ func errors(err error, c *fibre.Context) {
default: default:
c.Text(code, text) c.Text(code, text)
case "application/json": case "application/json":
c.JSON(code, errs[code]) info := errs[code]
info["information"] = text
c.JSON(code, info)
case "application/msgpack": 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{}{ 200: map[string]interface{}{
"code": 200, "code": 200,

View file

@ -77,12 +77,7 @@ func routes(s *fibre.Fibre) {
s.Post("/sql", func(c *fibre.Context) error { s.Post("/sql", func(c *fibre.Context) error {
res, err := db.Execute(c, c.Request().Body) res, err := db.Execute(c, c.Request().Body)
if err != nil { if err != nil {
return c.Send(500, map[string]interface{}{ return err
"code": 500,
"details": "Request problems detected",
"documentation": "https://docs.surreal.io/",
"information": err.Error(),
})
} }
return c.Send(200, res) return c.Send(200, res)
}) })