dcbot/internal/domain/error.go
Dawid Wysokiński e9c5e06407
All checks were successful
continuous-integration/drone/push Build is passing
refactor: rename domain.UserError -> domain.Error
2022-12-31 10:59:40 +01:00

64 lines
990 B
Go

package domain
import (
"errors"
"fmt"
)
var (
ErrNothingToUpdate = errors.New("nothing to update")
ErrRequired = errors.New("cannot be blank")
)
type ErrorCode uint8
const (
ErrorCodeUnknown ErrorCode = iota
ErrorCodeEntityNotFound
ErrorCodeValidationError
ErrorCodeAlreadyExists
)
type Error interface {
error
UserError() string
Code() ErrorCode
}
type ValidationError struct {
Field string
Err error
}
func (e ValidationError) Error() string {
return fmt.Sprintf("%s: %s", e.Field, e.Err)
}
func (e ValidationError) UserError() string {
return e.Error()
}
func (e ValidationError) Code() ErrorCode {
return ErrorCodeValidationError
}
func (e ValidationError) Unwrap() error {
return e.Err
}
type MinError struct {
Min int
}
func (e MinError) Error() string {
return fmt.Sprintf("must be no less than %d", e.Min)
}
func (e MinError) UserError() string {
return e.Error()
}
func (e MinError) Code() ErrorCode {
return ErrorCodeValidationError
}