customer/internal/errors/errors.go
2023-05-17 23:27:09 +03:00

57 lines
1.1 KiB
Go

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")
)
type Error interface {
Error() string
Extract() error
Type() ErrorType
Wrap(err error)
SetType(errorType ErrorType)
}
type customError struct {
errorType ErrorType
err error
}
func New(err error, errorType ErrorType) *customError {
return &customError{
errorType: errorType,
err: err,
}
}
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
}