surrealpatch/web/err.go

152 lines
3.9 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-14 21:33:09 +00:00
"github.com/abcum/surreal/sql"
2016-04-03 21:48:57 +00:00
)
2016-10-14 21:54:32 +00:00
func errors(val error, c *fibre.Context) {
2016-04-03 21:48:57 +00:00
2017-02-20 01:09:24 +00:00
if c.Socket() != nil {
return
}
2016-09-14 21:33:09 +00:00
var code int
2016-10-14 21:54:32 +00:00
var info string
2016-09-06 13:36:09 +00:00
2016-10-14 21:54:32 +00:00
switch e := val.(type) {
2016-09-06 13:36:09 +00:00
default:
2016-10-14 21:54:32 +00:00
code, info = 400, e.Error()
2017-02-20 01:09:24 +00:00
case *sql.PermsError:
code, info = 403, e.Error()
2017-02-20 01:09:24 +00:00
case *sql.BlankError:
code, info = 403, e.Error()
case *sql.QueryError:
code, info = 401, e.Error()
2016-09-14 21:33:09 +00:00
case *sql.ParseError:
2016-10-14 21:54:32 +00:00
code, info = 400, e.Error()
2016-09-14 21:33:09 +00:00
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-10-14 21:54:32 +00:00
c.Send(code, &err{
errs[code].Code,
errs[code].Details,
errs[code].Description,
info,
})
2016-04-03 21:48:57 +00:00
2016-10-14 21:54:32 +00:00
}
2016-09-14 21:33:09 +00:00
2016-10-14 21:54:32 +00:00
type err struct {
Code int `codec:"code,omitempty"`
Details string `codec:"details,omitempty"`
Description string `codec:"description,omitempty"`
Information string `codec:"information,omitempty"`
2016-04-03 21:48:57 +00:00
}
2016-10-14 21:54:32 +00:00
var errs = map[int]*err{
2016-04-03 21:48:57 +00:00
2016-11-16 17:31:25 +00:00
200: {
2016-10-14 21:54:32 +00:00
Code: 200,
Details: "Information",
Description: "Visit the documentation for details on accessing the api.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
400: {
2016-10-14 21:54:32 +00:00
Code: 400,
Details: "Request problems detected",
Description: "There is a problem with your request. Ensure that the request is valid.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
401: {
2016-10-14 21:54:32 +00:00
Code: 401,
Details: "Authentication failed",
Description: "Your authentication details are invalid. Reauthenticate using a valid token.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
403: {
2016-10-14 21:54:32 +00:00
Code: 403,
Details: "Request resource forbidden",
Description: "Your request was forbidden. Perhaps you don't have the necessary permissions to access this resource.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
404: {
2016-10-14 21:54:32 +00:00
Code: 404,
Details: "Request resource not found",
Description: "The requested resource does not exist. Check that you have entered the url correctly.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
405: {
2016-10-14 21:54:32 +00:00
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.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
409: {
2016-10-14 21:54:32 +00:00
Code: 409,
Details: "Request conflict detected",
Description: "The request could not be processed because of a conflict in the request.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
413: {
2016-10-14 21:54:32 +00:00
Code: 413,
Details: "Request content length too large",
Description: "All requests to the database must not exceed the predefined content length.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
415: {
2016-10-14 21:54:32 +00:00
Code: 415,
Details: "Unsupported content type requested",
Description: "The request needs to adhere to certain constraints. Check your request settings and try again.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
422: {
2016-10-14 21:54:32 +00:00
Code: 422,
Details: "Request problems detected",
Description: "There is a problem with your request. The request appears to contain invalid data.",
2016-04-03 21:48:57 +00:00
},
2016-11-16 17:31:25 +00:00
426: {
2016-10-14 21:54:32 +00:00
Code: 426,
Details: "Upgrade required",
Description: "There is a problem with your request. The request is expected to upgrade to a websocket connection.",
2016-06-18 09:00:23 +00:00
},
2016-11-16 17:31:25 +00:00
500: {
2017-02-10 19:55:46 +00:00
Code: 500,
Details: "Internal server error",
Description: "There was a problem with our servers, and we have been notified.",
},
2017-02-10 19:56:03 +00:00
501: {
Code: 501,
Details: "Not Implemented",
Description: "The server either does not recognize the request method, or it lacks the ability to fulfill the request.",
2016-04-03 21:48:57 +00:00
},
504: {
Code: 504,
Details: "Gateway Timeout",
Description: "The request to the server was restricted by a timeout duration, and did not manage to complete in time.",
},
2016-04-03 21:48:57 +00:00
}