sessions/internal/domain/error.go
Dawid Wysokiński 5b061dfef3
All checks were successful
continuous-integration/drone/push Build is passing
feat: add a new command - db user create (#4)
Reviewed-on: #4
2022-11-19 07:24:05 +00:00

94 lines
1.5 KiB
Go

package domain
import (
"errors"
"fmt"
)
var (
ErrRequired = errors.New("cannot be blank")
)
type ErrorCode uint8
const (
ErrorCodeUnknown ErrorCode = iota
ErrorCodeEntityNotFound
ErrorCodeValidationError
ErrorCodeAlreadyExists
)
func (e ErrorCode) String() string {
switch e {
case ErrorCodeEntityNotFound:
return "entity-not-found"
case ErrorCodeValidationError:
return "validation-error"
case ErrorCodeAlreadyExists:
return "already-exists"
case ErrorCodeUnknown:
fallthrough
default:
return "internal-server-error"
}
}
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 MinLengthError struct {
Min int
}
func (e MinLengthError) Error() string {
return fmt.Sprintf("the length must be no less than %d", e.Min)
}
func (e MinLengthError) UserError() string {
return e.Error()
}
func (e MinLengthError) Code() ErrorCode {
return ErrorCodeValidationError
}
type MaxLengthError struct {
Max int
}
func (e MaxLengthError) Error() string {
return fmt.Sprintf("the length must be no more than %d", e.Max)
}
func (e MaxLengthError) UserError() string {
return e.Error()
}
func (e MaxLengthError) Code() ErrorCode {
return ErrorCodeValidationError
}