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