sessions/internal/domain/error.go
Dawid Wysokiński 8e8e7e1f94
All checks were successful
continuous-integration/drone/push Build is passing
feat: add a new REST endpoint - PUT /api/v1/user/sessions/:server (#8)
Reviewed-on: #8
2022-11-24 05:17:32 +00:00

111 lines
1.8 KiB
Go

package domain
import (
"errors"
"fmt"
)
var (
ErrRequired = errors.New("cannot be blank")
ErrBase64 = errors.New("must be encoded in Base64")
)
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
}
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
}