48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package models
|
|
|
|
type APIError struct {
|
|
Code int `json:"-"`
|
|
Message string `json:"message"`
|
|
Slug string `json:"error"`
|
|
Cause error `json:"-"`
|
|
}
|
|
|
|
var (
|
|
ErrBadRequest = &APIError{Code: 400, Slug: "bad_request", Message: "Invalid request"}
|
|
ErrUnauthorized = &APIError{Code: 401, Slug: "unauthorized", Message: "Incorrect credentials"}
|
|
ErrForbidden = &APIError{Code: 403, Slug: "forbidden", Message: "Forbidden"}
|
|
ErrInsufficientScope = &APIError{Code: 403, Slug: "insufficient_scope", Message: "The access token does not contain the required scope"}
|
|
ErrNotFound = &APIError{Code: 404, Slug: "not_found", Message: "Resource not found"}
|
|
ErrAlreadyExists = &APIError{Code: 409, Slug: "already_exists", Message: "Resource already exists"}
|
|
ErrInternal = &APIError{Code: 500, Slug: "internal", Message: "Internal server error"}
|
|
)
|
|
|
|
func (e *APIError) Error() string {
|
|
return e.Slug
|
|
}
|
|
|
|
func (e *APIError) WithMessage(msg string) *APIError {
|
|
if e == nil {
|
|
return (&APIError{Code: 500, Slug: "internal", Message: "Internal server error"}).WithMessage(msg)
|
|
}
|
|
cp := *e
|
|
cp.Message = msg
|
|
return &cp
|
|
}
|
|
|
|
func (e *APIError) WithCause(cause error) *APIError {
|
|
if e == nil {
|
|
return (&APIError{Code: 500, Slug: "internal", Message: "Internal server error"}).WithCause(cause)
|
|
}
|
|
cp := *e
|
|
cp.Cause = cause
|
|
return &cp
|
|
}
|
|
|
|
func (e *APIError) Unwrap() error {
|
|
if e == nil {
|
|
return nil
|
|
}
|
|
return e.Cause
|
|
}
|