2023-05-17 20:27:09 +00:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ErrorType error
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInternalError ErrorType = errors.New("internal error")
|
|
|
|
ErrInvalidArgs ErrorType = errors.New("invalid arguments")
|
|
|
|
ErrMethodNotImplemented ErrorType = errors.New("method is not implemented")
|
|
|
|
ErrNotFound ErrorType = errors.New("record not found")
|
|
|
|
ErrNoAccess ErrorType = errors.New("no access")
|
2023-05-18 12:26:37 +00:00
|
|
|
ErrConflict ErrorType = errors.New("record already exist")
|
2023-05-30 11:33:57 +00:00
|
|
|
ErrInsufficientFunds ErrorType = errors.New("insufficient funds")
|
2023-05-17 20:27:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Error interface {
|
|
|
|
Error() string
|
|
|
|
Extract() error
|
|
|
|
Type() ErrorType
|
|
|
|
Wrap(err error)
|
|
|
|
SetType(errorType ErrorType)
|
|
|
|
}
|
|
|
|
|
|
|
|
type customError struct {
|
|
|
|
errorType ErrorType
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2023-05-19 05:25:25 +00:00
|
|
|
func New(err error, errorType ErrorType) Error {
|
2023-05-17 20:27:09 +00:00
|
|
|
return &customError{
|
|
|
|
errorType: errorType,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 12:19:51 +00:00
|
|
|
func NewWithMessage(message string, errorType ErrorType) Error {
|
|
|
|
return &customError{
|
|
|
|
errorType: errorType,
|
|
|
|
err: errors.New(message),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 20:27:09 +00:00
|
|
|
func (receiver *customError) Error() string {
|
|
|
|
return receiver.err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (receiver *customError) Type() ErrorType {
|
|
|
|
return receiver.errorType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (receiver *customError) Wrap(err error) {
|
|
|
|
receiver.err = fmt.Errorf("%w: %w", receiver.err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (receiver *customError) SetType(errorType ErrorType) {
|
|
|
|
receiver.errorType = errorType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (receiver *customError) Extract() error {
|
|
|
|
return receiver.err
|
|
|
|
}
|