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 }