Improve web errors

This commit is contained in:
Tobie Morgan Hitchcock 2016-10-14 22:54:32 +01:00
parent b5ff2cc1cb
commit f36404158d

View file

@ -20,25 +20,24 @@ import (
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
) )
func errors(err error, c *fibre.Context) { func errors(val error, c *fibre.Context) {
var code int var code int
var text string var info string
var info map[string]interface{}
switch e := err.(type) { switch e := val.(type) {
default: default:
code = 400 code, info = 400, e.Error()
case *kvs.DBError: case *kvs.DBError:
code, text = 503, e.Error() code, info = 503, e.Error()
case *kvs.TXError: case *kvs.TXError:
code, text = 500, e.Error() code, info = 500, e.Error()
case *kvs.KVError: case *kvs.KVError:
code, text = 409, e.Error() code, info = 409, e.Error()
case *kvs.CKError: case *kvs.CKError:
code, text = 403, e.Error() code, info = 403, e.Error()
case *sql.ParseError: case *sql.ParseError:
code, text = 400, e.Error() code, info = 400, e.Error()
case *fibre.HTTPError: case *fibre.HTTPError:
code = e.Code() code = e.Code()
} }
@ -47,97 +46,92 @@ func errors(err error, c *fibre.Context) {
code = 500 code = 500
} }
info = errs[code] c.Send(code, &err{
if text != "" { errs[code].Code,
info["information"] = text errs[code].Details,
} errs[code].Description,
info,
c.Send(code, info) })
} }
var errs = map[int]map[string]interface{}{ type err struct {
Code int `codec:"code,omitempty"`
Details string `codec:"details,omitempty"`
Description string `codec:"description,omitempty"`
Information string `codec:"information,omitempty"`
}
200: map[string]interface{}{ var errs = map[int]*err{
"code": 200,
"details": "Information", 200: &err{
"documentation": "https://docs.surreal.io/", Code: 200,
"information": "Visit the documentation for details on accessing the api.", Details: "Information",
Description: "Visit the documentation for details on accessing the api.",
}, },
400: map[string]interface{}{ 400: &err{
"code": 400, Code: 400,
"details": "Request problems detected", Details: "Request problems detected",
"documentation": "https://docs.surreal.io/", Description: "There is a problem with your request. Ensure that the request is valid.",
"information": "There is a problem with your request. Ensure that the request is valid.",
}, },
401: map[string]interface{}{ 401: &err{
"code": 401, Code: 401,
"details": "Authentication failed", Details: "Authentication failed",
"documentation": "https://docs.surreal.io/", Description: "Your authentication details are invalid. Reauthenticate using a valid token.",
"information": "Your authentication details are invalid. Reauthenticate using a valid token.",
}, },
403: map[string]interface{}{ 403: &err{
"code": 403, Code: 403,
"details": "Request resource forbidden", Details: "Request resource forbidden",
"documentation": "https://docs.surreal.io/", Description: "Your request was forbidden. Perhaps you don't have the necessary permissions to access this resource.",
"information": "Your request was forbidden. Perhaps you don't have the necessary permissions to access this resource.",
}, },
404: map[string]interface{}{ 404: &err{
"code": 404, Code: 404,
"details": "Request resource not found", Details: "Request resource not found",
"documentation": "https://docs.surreal.io/", Description: "The requested resource does not exist. Check that you have entered the url correctly.",
"information": "The requested resource does not exist. Check that you have entered the url correctly.",
}, },
405: map[string]interface{}{ 405: &err{
"code": 405, Code: 405,
"details": "This method is not allowed", Details: "This method is not allowed",
"documentation": "https://docs.surreal.io/", Description: "The requested http method is not allowed for this resource. Refer to the documentation for allowed methods.",
"information": "The requested http method is not allowed for this resource. Refer to the documentation for allowed methods.",
}, },
409: map[string]interface{}{ 409: &err{
"code": 409, Code: 409,
"details": "Request conflict detected", Details: "Request conflict detected",
"documentation": "https://docs.surreal.io/", Description: "The request could not be processed because of a conflict in the request.",
"information": "The request could not be processed because of a conflict in the request.",
}, },
413: map[string]interface{}{ 413: &err{
"code": 413, Code: 413,
"details": "Request content length too large", Details: "Request content length too large",
"documentation": "https://docs.surreal.io/", Description: "All requests to the database must not exceed the predefined content length.",
"information": "All requests to the database must not exceed the predefined content length.",
}, },
415: map[string]interface{}{ 415: &err{
"code": 415, Code: 415,
"details": "Unsupported content type requested", Details: "Unsupported content type requested",
"documentation": "https://docs.surreal.io/", Description: "The request needs to adhere to certain constraints. Check your request settings and try again.",
"information": "The request needs to adhere to certain constraints. Check your request settings and try again.",
}, },
422: map[string]interface{}{ 422: &err{
"code": 422, Code: 422,
"details": "Request problems detected", Details: "Request problems detected",
"documentation": "https://docs.surreal.io/", Description: "There is a problem with your request. The request appears to contain invalid data.",
"information": "There is a problem with your request. The request appears to contain invalid data.",
}, },
426: map[string]interface{}{ 426: &err{
"code": 426, Code: 426,
"details": "Upgrade required", Details: "Upgrade required",
"documentation": "https://docs.surreal.io/", Description: "There is a problem with your request. The request is expected to upgrade to a websocket connection.",
"information": "There is a problem with your request. The request is expected to upgrade to a websocket connection.",
}, },
500: map[string]interface{}{ 500: &err{
"code": 500, Code: 500,
"details": "There was a problem with our servers, and we have been notified", Details: "There was a problem with our servers, and we have been notified",
"documentation": "https://docs.surreal.io/",
}, },
} }