refactor: rename domain.Error methods (Code -> Type, Slug -> Code)
This commit is contained in:
parent
a66bbb0b2f
commit
186c8d6765
|
@ -54,11 +54,11 @@ components:
|
|||
Error:
|
||||
type: object
|
||||
required:
|
||||
- slug
|
||||
- code
|
||||
- message
|
||||
additionalProperties: false
|
||||
properties:
|
||||
slug:
|
||||
code:
|
||||
type: string
|
||||
example: length-out-of-range
|
||||
message:
|
||||
|
|
|
@ -1,30 +1,30 @@
|
|||
package domain
|
||||
|
||||
type ErrorCode uint8
|
||||
type ErrorType uint8
|
||||
|
||||
const (
|
||||
ErrorCodeUnknown ErrorCode = iota
|
||||
ErrorCodeNotFound
|
||||
ErrorCodeIncorrectInput
|
||||
ErrorTypeUnknown ErrorType = iota
|
||||
ErrorTypeNotFound
|
||||
ErrorTypeIncorrectInput
|
||||
)
|
||||
|
||||
func (e ErrorCode) String() string {
|
||||
func (e ErrorType) String() string {
|
||||
switch e {
|
||||
case ErrorCodeNotFound:
|
||||
return "not-found"
|
||||
case ErrorCodeIncorrectInput:
|
||||
return "incorrect-input"
|
||||
case ErrorCodeUnknown:
|
||||
case ErrorTypeNotFound:
|
||||
return "not found"
|
||||
case ErrorTypeIncorrectInput:
|
||||
return "incorrect input"
|
||||
case ErrorTypeUnknown:
|
||||
fallthrough
|
||||
default:
|
||||
return "unknown-error"
|
||||
return "unknown error"
|
||||
}
|
||||
}
|
||||
|
||||
type Error interface {
|
||||
error
|
||||
Code() ErrorCode
|
||||
Slug() string
|
||||
Type() ErrorType
|
||||
Code() string
|
||||
}
|
||||
|
||||
type ErrorWithParams interface {
|
||||
|
@ -34,8 +34,8 @@ type ErrorWithParams interface {
|
|||
|
||||
type simpleError struct {
|
||||
msg string
|
||||
code ErrorCode
|
||||
slug string
|
||||
typ ErrorType
|
||||
code string
|
||||
}
|
||||
|
||||
var _ Error = simpleError{}
|
||||
|
@ -44,10 +44,10 @@ func (e simpleError) Error() string {
|
|||
return e.msg
|
||||
}
|
||||
|
||||
func (e simpleError) Code() ErrorCode {
|
||||
return e.code
|
||||
func (e simpleError) Type() ErrorType {
|
||||
return e.typ
|
||||
}
|
||||
|
||||
func (e simpleError) Slug() string {
|
||||
return e.slug
|
||||
func (e simpleError) Code() string {
|
||||
return e.code
|
||||
}
|
||||
|
|
|
@ -625,11 +625,11 @@ func (e ServerNotFoundError) Error() string {
|
|||
return fmt.Sprintf("server with key %s not found", e.Key)
|
||||
}
|
||||
|
||||
func (e ServerNotFoundError) Code() ErrorCode {
|
||||
return ErrorCodeNotFound
|
||||
func (e ServerNotFoundError) Type() ErrorType {
|
||||
return ErrorTypeNotFound
|
||||
}
|
||||
|
||||
func (e ServerNotFoundError) Slug() string {
|
||||
func (e ServerNotFoundError) Code() string {
|
||||
return "server-not-found"
|
||||
}
|
||||
|
||||
|
|
|
@ -37,19 +37,19 @@ func (e ValidationError) Path() string {
|
|||
return path
|
||||
}
|
||||
|
||||
func (e ValidationError) Code() ErrorCode {
|
||||
func (e ValidationError) Type() ErrorType {
|
||||
var domainErr Error
|
||||
if errors.As(e.Err, &domainErr) {
|
||||
return domainErr.Type()
|
||||
}
|
||||
return ErrorTypeIncorrectInput
|
||||
}
|
||||
|
||||
func (e ValidationError) Code() string {
|
||||
var domainErr Error
|
||||
if errors.As(e.Err, &domainErr) {
|
||||
return domainErr.Code()
|
||||
}
|
||||
return ErrorCodeIncorrectInput
|
||||
}
|
||||
|
||||
func (e ValidationError) Slug() string {
|
||||
var domainErr Error
|
||||
if errors.As(e.Err, &domainErr) {
|
||||
return domainErr.Slug()
|
||||
}
|
||||
return "validation-failed"
|
||||
}
|
||||
|
||||
|
@ -97,19 +97,19 @@ func (e SliceElementValidationError) Path() string {
|
|||
return path
|
||||
}
|
||||
|
||||
func (e SliceElementValidationError) Code() ErrorCode {
|
||||
func (e SliceElementValidationError) Type() ErrorType {
|
||||
var domainErr Error
|
||||
if errors.As(e.Err, &domainErr) {
|
||||
return domainErr.Type()
|
||||
}
|
||||
return ErrorTypeIncorrectInput
|
||||
}
|
||||
|
||||
func (e SliceElementValidationError) Code() string {
|
||||
var domainErr Error
|
||||
if errors.As(e.Err, &domainErr) {
|
||||
return domainErr.Code()
|
||||
}
|
||||
return ErrorCodeIncorrectInput
|
||||
}
|
||||
|
||||
func (e SliceElementValidationError) Slug() string {
|
||||
var domainErr Error
|
||||
if errors.As(e.Err, &domainErr) {
|
||||
return domainErr.Slug()
|
||||
}
|
||||
return "slice-element-validation-failed"
|
||||
}
|
||||
|
||||
|
@ -136,11 +136,11 @@ func (e MinGreaterEqualError) Error() string {
|
|||
return fmt.Sprintf("must be no less than %d (current: %d)", e.Min, e.Current)
|
||||
}
|
||||
|
||||
func (e MinGreaterEqualError) Code() ErrorCode {
|
||||
return ErrorCodeIncorrectInput
|
||||
func (e MinGreaterEqualError) Type() ErrorType {
|
||||
return ErrorTypeIncorrectInput
|
||||
}
|
||||
|
||||
func (e MinGreaterEqualError) Slug() string {
|
||||
func (e MinGreaterEqualError) Code() string {
|
||||
return "min-greater-equal"
|
||||
}
|
||||
|
||||
|
@ -162,11 +162,11 @@ func (e MaxLessEqualError) Error() string {
|
|||
return fmt.Sprintf("must be no greater than %d (current: %d)", e.Max, e.Current)
|
||||
}
|
||||
|
||||
func (e MaxLessEqualError) Code() ErrorCode {
|
||||
return ErrorCodeIncorrectInput
|
||||
func (e MaxLessEqualError) Type() ErrorType {
|
||||
return ErrorTypeIncorrectInput
|
||||
}
|
||||
|
||||
func (e MaxLessEqualError) Slug() string {
|
||||
func (e MaxLessEqualError) Code() string {
|
||||
return "max-less-equal"
|
||||
}
|
||||
|
||||
|
@ -189,11 +189,11 @@ func (e LenOutOfRangeError) Error() string {
|
|||
return fmt.Sprintf("length must be between %d and %d (current length: %d)", e.Min, e.Max, e.Current)
|
||||
}
|
||||
|
||||
func (e LenOutOfRangeError) Code() ErrorCode {
|
||||
return ErrorCodeIncorrectInput
|
||||
func (e LenOutOfRangeError) Type() ErrorType {
|
||||
return ErrorTypeIncorrectInput
|
||||
}
|
||||
|
||||
func (e LenOutOfRangeError) Slug() string {
|
||||
func (e LenOutOfRangeError) Code() string {
|
||||
return "length-out-of-range"
|
||||
}
|
||||
|
||||
|
@ -215,11 +215,11 @@ func (e InvalidURLError) Error() string {
|
|||
return fmt.Sprintf("%s: invalid URL", e.URL)
|
||||
}
|
||||
|
||||
func (e InvalidURLError) Code() ErrorCode {
|
||||
return ErrorCodeIncorrectInput
|
||||
func (e InvalidURLError) Type() ErrorType {
|
||||
return ErrorTypeIncorrectInput
|
||||
}
|
||||
|
||||
func (e InvalidURLError) Slug() string {
|
||||
func (e InvalidURLError) Code() string {
|
||||
return "invalid-url"
|
||||
}
|
||||
|
||||
|
@ -231,20 +231,20 @@ func (e InvalidURLError) Params() map[string]any {
|
|||
|
||||
var ErrRequired error = simpleError{
|
||||
msg: "can't be blank",
|
||||
code: ErrorCodeIncorrectInput,
|
||||
slug: "required",
|
||||
typ: ErrorTypeIncorrectInput,
|
||||
code: "required",
|
||||
}
|
||||
|
||||
var ErrNil error = simpleError{
|
||||
msg: "must not be nil",
|
||||
code: ErrorCodeIncorrectInput,
|
||||
slug: "nil",
|
||||
typ: ErrorTypeIncorrectInput,
|
||||
code: "nil",
|
||||
}
|
||||
|
||||
var ErrInvalidCursor error = simpleError{
|
||||
msg: "invalid cursor",
|
||||
code: ErrorCodeIncorrectInput,
|
||||
slug: "invalid-cursor",
|
||||
typ: ErrorTypeIncorrectInput,
|
||||
code: "invalid-cursor",
|
||||
}
|
||||
|
||||
func validateSliceLen[S ~[]E, E any](s S, min, max int) error {
|
||||
|
|
|
@ -328,11 +328,11 @@ func (e VersionNotFoundError) Error() string {
|
|||
return fmt.Sprintf("version with code %s not found", e.VersionCode)
|
||||
}
|
||||
|
||||
func (e VersionNotFoundError) Code() ErrorCode {
|
||||
return ErrorCodeNotFound
|
||||
func (e VersionNotFoundError) Type() ErrorType {
|
||||
return ErrorTypeNotFound
|
||||
}
|
||||
|
||||
func (e VersionNotFoundError) Slug() string {
|
||||
func (e VersionNotFoundError) Code() string {
|
||||
return "version-not-found"
|
||||
}
|
||||
|
||||
|
|
|
@ -69,7 +69,7 @@ func (h *apiHTTPHandler) handleNotFound(w http.ResponseWriter, r *http.Request)
|
|||
errors: []error{
|
||||
apiError{
|
||||
status: http.StatusNotFound,
|
||||
slug: "route-not-found",
|
||||
code: "route-not-found",
|
||||
message: "route not found",
|
||||
},
|
||||
},
|
||||
|
@ -81,7 +81,7 @@ func (h *apiHTTPHandler) handleMethodNotAllowed(w http.ResponseWriter, r *http.R
|
|||
errors: []error{
|
||||
apiError{
|
||||
status: http.StatusMethodNotAllowed,
|
||||
slug: "method-not-allowed",
|
||||
code: "method-not-allowed",
|
||||
message: "method not allowed",
|
||||
},
|
||||
},
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
type apiError struct {
|
||||
status int
|
||||
slug string
|
||||
code string
|
||||
path []string
|
||||
params map[string]any
|
||||
message string
|
||||
|
@ -28,7 +28,7 @@ func (e apiError) toResponse() apimodel.ErrorResponse {
|
|||
Message: e.message,
|
||||
Params: e.params,
|
||||
Path: e.path,
|
||||
Slug: e.slug,
|
||||
Code: e.code,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ type apiErrorRenderer struct {
|
|||
|
||||
var errInternalServerError = apiError{
|
||||
status: http.StatusInternalServerError,
|
||||
slug: "internal-server-error",
|
||||
code: "internal-server-error",
|
||||
message: "internal server error",
|
||||
}
|
||||
|
||||
|
@ -97,7 +97,7 @@ func (re apiErrorRenderer) render(w http.ResponseWriter, r *http.Request) {
|
|||
case errors.As(err, ¶mFormatErr):
|
||||
apiErr = apiError{
|
||||
status: http.StatusBadRequest,
|
||||
slug: "invalid-param-format",
|
||||
code: "invalid-param-format",
|
||||
path: []string{"$query", paramFormatErr.ParamName},
|
||||
message: paramFormatErr.Err.Error(),
|
||||
}
|
||||
|
@ -169,21 +169,21 @@ func (re apiErrorRenderer) domainErrorToAPIError(domainErr domain.Error) apiErro
|
|||
}
|
||||
|
||||
return apiError{
|
||||
status: errorCodeToStatusCode(domainErr.Code()),
|
||||
slug: domainErr.Slug(),
|
||||
status: errorTypeToStatusCode(domainErr.Type()),
|
||||
code: domainErr.Code(),
|
||||
path: path,
|
||||
params: cloned,
|
||||
message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func errorCodeToStatusCode(code domain.ErrorCode) int {
|
||||
func errorTypeToStatusCode(code domain.ErrorType) int {
|
||||
switch code {
|
||||
case domain.ErrorCodeIncorrectInput:
|
||||
case domain.ErrorTypeIncorrectInput:
|
||||
return http.StatusBadRequest
|
||||
case domain.ErrorCodeNotFound:
|
||||
case domain.ErrorTypeNotFound:
|
||||
return http.StatusNotFound
|
||||
case domain.ErrorCodeUnknown:
|
||||
case domain.ErrorTypeUnknown:
|
||||
fallthrough
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
|
|
Loading…
Reference in New Issue