2015-01-13 23:31:27 +00:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2016-01-30 21:16:45 +00:00
|
|
|
// Errors provide a way to return detailed information
|
|
|
|
// for an RPC request error. The error is normally
|
|
|
|
// JSON encoded.
|
2015-01-13 23:31:27 +00:00
|
|
|
type Error struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Code int32 `json:"code"`
|
|
|
|
Detail string `json:"detail"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
b, _ := json.Marshal(e)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(id, detail string, code int32) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: code,
|
|
|
|
Detail: detail,
|
|
|
|
Status: http.StatusText(int(code)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(err string) *Error {
|
2015-01-30 15:51:16 +01:00
|
|
|
e := new(Error)
|
|
|
|
errr := json.Unmarshal([]byte(err), e)
|
2015-01-13 23:31:27 +00:00
|
|
|
if errr != nil {
|
|
|
|
e.Detail = err
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func BadRequest(id, detail string) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 400,
|
|
|
|
Detail: detail,
|
|
|
|
Status: http.StatusText(400),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Unauthorized(id, detail string) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 401,
|
|
|
|
Detail: detail,
|
|
|
|
Status: http.StatusText(401),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Forbidden(id, detail string) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 403,
|
|
|
|
Detail: detail,
|
|
|
|
Status: http.StatusText(403),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotFound(id, detail string) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 404,
|
|
|
|
Detail: detail,
|
|
|
|
Status: http.StatusText(404),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func InternalServerError(id, detail string) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 500,
|
|
|
|
Detail: detail,
|
|
|
|
Status: http.StatusText(500),
|
|
|
|
}
|
|
|
|
}
|