2017-05-31 15:29:03 +02:00
|
|
|
// Package errors provides a way to return detailed information
|
|
|
|
// for an RPC request error. The error is normally JSON encoded.
|
2015-01-14 01:31:27 +02:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-05-31 15:29:03 +02:00
|
|
|
"fmt"
|
2015-01-14 01:31:27 +02:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2020-02-06 12:18:33 +02:00
|
|
|
//go:generate protoc -I. --go_out=paths=source_relative:. errors.proto
|
2015-01-14 01:31:27 +02:00
|
|
|
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
b, _ := json.Marshal(e)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2017-05-31 15:29:03 +02:00
|
|
|
// New generates a custom error.
|
2015-01-14 01:31:27 +02:00
|
|
|
func New(id, detail string, code int32) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: code,
|
|
|
|
Detail: detail,
|
|
|
|
Status: http.StatusText(int(code)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 15:29:03 +02:00
|
|
|
// Parse tries to parse a JSON string into an error. If that
|
|
|
|
// fails, it will set the given string as the error detail.
|
2015-01-14 01:31:27 +02:00
|
|
|
func Parse(err string) *Error {
|
2015-01-30 16:51:16 +02:00
|
|
|
e := new(Error)
|
|
|
|
errr := json.Unmarshal([]byte(err), e)
|
2015-01-14 01:31:27 +02:00
|
|
|
if errr != nil {
|
|
|
|
e.Detail = err
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-31 15:29:03 +02:00
|
|
|
// BadRequest generates a 400 error.
|
|
|
|
func BadRequest(id, format string, a ...interface{}) error {
|
2015-01-14 01:31:27 +02:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 400,
|
2017-05-31 15:29:03 +02:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2015-01-14 01:31:27 +02:00
|
|
|
Status: http.StatusText(400),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 15:29:03 +02:00
|
|
|
// Unauthorized generates a 401 error.
|
|
|
|
func Unauthorized(id, format string, a ...interface{}) error {
|
2015-01-14 01:31:27 +02:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 401,
|
2017-05-31 15:29:03 +02:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2015-01-14 01:31:27 +02:00
|
|
|
Status: http.StatusText(401),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 15:29:03 +02:00
|
|
|
// Forbidden generates a 403 error.
|
|
|
|
func Forbidden(id, format string, a ...interface{}) error {
|
2015-01-14 01:31:27 +02:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 403,
|
2017-05-31 15:29:03 +02:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2015-01-14 01:31:27 +02:00
|
|
|
Status: http.StatusText(403),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 15:29:03 +02:00
|
|
|
// NotFound generates a 404 error.
|
|
|
|
func NotFound(id, format string, a ...interface{}) error {
|
2015-01-14 01:31:27 +02:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 404,
|
2017-05-31 15:29:03 +02:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2015-01-14 01:31:27 +02:00
|
|
|
Status: http.StatusText(404),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:42:34 +02:00
|
|
|
// MethodNotAllowed generates a 405 error.
|
|
|
|
func MethodNotAllowed(id, format string, a ...interface{}) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 405,
|
|
|
|
Detail: fmt.Sprintf(format, a...),
|
|
|
|
Status: http.StatusText(405),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-25 11:41:28 +02:00
|
|
|
// Timeout generates a 408 error.
|
|
|
|
func Timeout(id, format string, a ...interface{}) error {
|
2015-01-14 01:31:27 +02:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
2018-11-25 11:41:28 +02:00
|
|
|
Code: 408,
|
2017-05-31 15:29:03 +02:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2018-11-25 11:41:28 +02:00
|
|
|
Status: http.StatusText(408),
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-11 15:51:33 +02:00
|
|
|
|
|
|
|
// Conflict generates a 409 error.
|
|
|
|
func Conflict(id, format string, a ...interface{}) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 409,
|
|
|
|
Detail: fmt.Sprintf(format, a...),
|
|
|
|
Status: http.StatusText(409),
|
|
|
|
}
|
|
|
|
}
|
2018-11-25 11:41:28 +02:00
|
|
|
|
|
|
|
// InternalServerError generates a 500 error.
|
|
|
|
func InternalServerError(id, format string, a ...interface{}) error {
|
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 500,
|
|
|
|
Detail: fmt.Sprintf(format, a...),
|
|
|
|
Status: http.StatusText(500),
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 13:27:20 +02:00
|
|
|
|
|
|
|
func Equal(err1 error, err2 error) bool {
|
|
|
|
verr1, ok1 := err1.(*Error)
|
|
|
|
verr2, ok2 := err2.(*Error)
|
|
|
|
|
|
|
|
if ok1 != ok2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok1 {
|
|
|
|
return err1 == err2
|
|
|
|
}
|
|
|
|
|
|
|
|
if verr1.Code != verr2.Code {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|