surrealpatch/web/err.go

144 lines
4.3 KiB
Go
Raw Normal View History

2016-04-03 21:48:57 +00:00
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
import (
"github.com/abcum/fibre"
2016-09-06 13:36:09 +00:00
"github.com/abcum/surreal/kvs"
2016-09-14 21:33:09 +00:00
"github.com/abcum/surreal/sql"
2016-04-03 21:48:57 +00:00
)
func errors(err error, c *fibre.Context) {
2016-09-14 21:33:09 +00:00
var code int
var text string
var info map[string]interface{}
2016-09-06 13:36:09 +00:00
2016-09-14 21:33:09 +00:00
switch e := err.(type) {
2016-09-06 13:36:09 +00:00
default:
2016-09-14 21:33:09 +00:00
code = 400
2016-09-06 13:36:09 +00:00
case *kvs.DBError:
2016-09-14 21:33:09 +00:00
code, text = 503, e.Error()
2016-09-06 13:36:09 +00:00
case *kvs.TXError:
2016-09-14 21:33:09 +00:00
code, text = 500, e.Error()
2016-09-06 13:36:09 +00:00
case *kvs.KVError:
2016-09-14 21:33:09 +00:00
code, text = 409, e.Error()
2016-09-06 13:36:09 +00:00
case *kvs.CKError:
2016-09-14 21:33:09 +00:00
code, text = 403, e.Error()
case *sql.ParseError:
code, text = 400, e.Error()
case *fibre.HTTPError:
code = e.Code()
2016-09-06 13:36:09 +00:00
}
2016-04-03 21:48:57 +00:00
2016-09-14 21:33:09 +00:00
if _, ok := errs[code]; !ok {
code = 500
2016-04-03 21:48:57 +00:00
}
2016-09-14 21:33:09 +00:00
info = errs[code]
if text != "" {
2016-09-06 13:36:09 +00:00
info["information"] = text
2016-04-03 21:48:57 +00:00
}
2016-09-14 21:33:09 +00:00
c.Send(code, info)
2016-04-03 21:48:57 +00:00
}
2016-09-06 13:36:09 +00:00
var errs = map[int]map[string]interface{}{
2016-04-03 21:48:57 +00:00
200: map[string]interface{}{
"code": 200,
"details": "Information",
"documentation": "https://docs.surreal.io/",
"information": "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.",
},
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.",
},
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.",
},
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.",
},
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.",
},
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.",
},
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.",
},
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.",
},
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.",
},
2016-06-18 09:00:23 +00:00
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.",
},
2016-04-03 21:48:57 +00:00
500: map[string]interface{}{
"code": 500,
"details": "There was a problem with our servers, and we have been notified",
"documentation": "https://docs.surreal.io/",
},
}