2017-05-31 16:29:03 +03:00
|
|
|
// Package errors provides a way to return detailed information
|
|
|
|
// for an RPC request error. The error is normally JSON encoded.
|
2015-01-14 02:31:27 +03:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-05-31 16:29:03 +03:00
|
|
|
"fmt"
|
2015-01-14 02:31:27 +03:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2020-09-28 13:08:53 +03:00
|
|
|
var (
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrBadRequest returns then requests contains invalid data
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrBadRequest = &Error{Code: 400}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrUnauthorized returns then user have unauthorized call
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrUnauthorized = &Error{Code: 401}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrForbidden returns then user have not access the resource
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrForbidden = &Error{Code: 403}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrNotFound returns then user specify invalid endpoint
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrNotFound = &Error{Code: 404}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrMethodNotAllowed returns then user try to get invalid method
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrMethodNotAllowed = &Error{Code: 405}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrTimeout returns then timeout exceeded
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrTimeout = &Error{Code: 408}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrConflict returns then request create duplicate resource
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrConflict = &Error{Code: 409}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrInternalServerError returns then server cant process request because of internal error
|
2020-09-28 13:08:53 +03:00
|
|
|
ErrInternalServerError = &Error{Code: 500}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErNotImplemented returns then server does not have desired endpoint method
|
2020-12-08 00:38:37 +03:00
|
|
|
ErNotImplemented = &Error{Code: 501}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrBadGateway returns then server cant process request
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrBadGateway = &Error{Code: 502}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrServiceUnavailable returns then service unavailable
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrServiceUnavailable = &Error{Code: 503}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrGatewayTimeout returns then server have long time to process request
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrGatewayTimeout = &Error{Code: 504}
|
2020-09-28 13:08:53 +03:00
|
|
|
)
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Error type
|
2020-07-19 11:29:48 +03:00
|
|
|
type Error struct {
|
2021-08-20 22:40:48 +03:00
|
|
|
// Id holds error id or service, usually someting like my_service or id
|
2021-03-06 19:45:13 +03:00
|
|
|
Id string
|
|
|
|
// Detail holds some useful details about error
|
2020-07-19 11:29:48 +03:00
|
|
|
Detail string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Status usually holds text of http status
|
2020-07-19 11:29:48 +03:00
|
|
|
Status string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Code holds error code
|
|
|
|
Code int32
|
2020-07-19 11:29:48 +03:00
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Error satisfies error interface
|
2015-01-14 02:31:27 +03:00
|
|
|
func (e *Error) Error() string {
|
|
|
|
b, _ := json.Marshal(e)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2021-03-06 19:45:13 +03:00
|
|
|
// New generates a custom error
|
2015-01-14 02:31:27 +03: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 16:29:03 +03: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 02:31:27 +03:00
|
|
|
func Parse(err string) *Error {
|
2015-01-30 17:51:16 +03:00
|
|
|
e := new(Error)
|
|
|
|
errr := json.Unmarshal([]byte(err), e)
|
2015-01-14 02:31:27 +03:00
|
|
|
if errr != nil {
|
|
|
|
e.Detail = err
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:03 +03:00
|
|
|
// BadRequest generates a 400 error.
|
|
|
|
func BadRequest(id, format string, a ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 400,
|
2017-05-31 16:29:03 +03:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2015-01-14 02:31:27 +03:00
|
|
|
Status: http.StatusText(400),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:03 +03:00
|
|
|
// Unauthorized generates a 401 error.
|
|
|
|
func Unauthorized(id, format string, a ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 401,
|
2017-05-31 16:29:03 +03:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2015-01-14 02:31:27 +03:00
|
|
|
Status: http.StatusText(401),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:03 +03:00
|
|
|
// Forbidden generates a 403 error.
|
|
|
|
func Forbidden(id, format string, a ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 403,
|
2017-05-31 16:29:03 +03:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2015-01-14 02:31:27 +03:00
|
|
|
Status: http.StatusText(403),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:03 +03:00
|
|
|
// NotFound generates a 404 error.
|
|
|
|
func NotFound(id, format string, a ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
|
|
|
Code: 404,
|
2017-05-31 16:29:03 +03:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2015-01-14 02:31:27 +03:00
|
|
|
Status: http.StatusText(404),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:42:34 +03: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 12:41:28 +03:00
|
|
|
// Timeout generates a 408 error.
|
|
|
|
func Timeout(id, format string, a ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
|
|
|
Id: id,
|
2018-11-25 12:41:28 +03:00
|
|
|
Code: 408,
|
2017-05-31 16:29:03 +03:00
|
|
|
Detail: fmt.Sprintf(format, a...),
|
2018-11-25 12:41:28 +03:00
|
|
|
Status: http.StatusText(408),
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-11 16:51:33 +03: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 12:41:28 +03: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 14:27:20 +03:00
|
|
|
|
2020-07-06 22:14:59 +03:00
|
|
|
// NotImplemented generates a 501 error
|
|
|
|
func NotImplemented(id, format string, a ...interface{}) error {
|
|
|
|
return &Error{
|
2020-07-19 11:29:48 +03:00
|
|
|
Id: id,
|
|
|
|
Code: 501,
|
|
|
|
Detail: fmt.Sprintf(format, a...),
|
|
|
|
Status: http.StatusText(501),
|
2020-07-06 22:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BadGateway generates a 502 error
|
|
|
|
func BadGateway(id, format string, a ...interface{}) error {
|
|
|
|
return &Error{
|
2020-07-19 11:29:48 +03:00
|
|
|
Id: id,
|
|
|
|
Code: 502,
|
|
|
|
Detail: fmt.Sprintf(format, a...),
|
|
|
|
Status: http.StatusText(502),
|
2020-07-06 22:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceUnavailable generates a 503 error
|
|
|
|
func ServiceUnavailable(id, format string, a ...interface{}) error {
|
|
|
|
return &Error{
|
2020-07-19 11:29:48 +03:00
|
|
|
Id: id,
|
|
|
|
Code: 503,
|
|
|
|
Detail: fmt.Sprintf(format, a...),
|
|
|
|
Status: http.StatusText(503),
|
2020-07-06 22:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GatewayTimeout generates a 504 error
|
|
|
|
func GatewayTimeout(id, format string, a ...interface{}) error {
|
|
|
|
return &Error{
|
2020-07-19 11:29:48 +03:00
|
|
|
Id: id,
|
|
|
|
Code: 504,
|
|
|
|
Detail: fmt.Sprintf(format, a...),
|
|
|
|
Status: http.StatusText(504),
|
2020-07-06 22:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:10:38 +03:00
|
|
|
// Equal tries to compare errors
|
2020-03-17 14:27:20 +03: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
|
|
|
|
}
|
2020-03-18 03:10:38 +03:00
|
|
|
|
|
|
|
// FromError try to convert go error to *Error
|
|
|
|
func FromError(err error) *Error {
|
|
|
|
if verr, ok := err.(*Error); ok && verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
|
|
|
|
|
|
|
return Parse(err.Error())
|
|
|
|
}
|