refactor: rename domain.Error methods (Code -> Type, Slug -> Code)

This commit is contained in:
Dawid Wysokiński 2024-02-01 07:48:03 +01:00
parent a66bbb0b2f
commit 186c8d6765
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
7 changed files with 75 additions and 75 deletions

View File

@ -54,11 +54,11 @@ components:
Error: Error:
type: object type: object
required: required:
- slug - code
- message - message
additionalProperties: false additionalProperties: false
properties: properties:
slug: code:
type: string type: string
example: length-out-of-range example: length-out-of-range
message: message:

View File

@ -1,30 +1,30 @@
package domain package domain
type ErrorCode uint8 type ErrorType uint8
const ( const (
ErrorCodeUnknown ErrorCode = iota ErrorTypeUnknown ErrorType = iota
ErrorCodeNotFound ErrorTypeNotFound
ErrorCodeIncorrectInput ErrorTypeIncorrectInput
) )
func (e ErrorCode) String() string { func (e ErrorType) String() string {
switch e { switch e {
case ErrorCodeNotFound: case ErrorTypeNotFound:
return "not-found" return "not found"
case ErrorCodeIncorrectInput: case ErrorTypeIncorrectInput:
return "incorrect-input" return "incorrect input"
case ErrorCodeUnknown: case ErrorTypeUnknown:
fallthrough fallthrough
default: default:
return "unknown-error" return "unknown error"
} }
} }
type Error interface { type Error interface {
error error
Code() ErrorCode Type() ErrorType
Slug() string Code() string
} }
type ErrorWithParams interface { type ErrorWithParams interface {
@ -34,8 +34,8 @@ type ErrorWithParams interface {
type simpleError struct { type simpleError struct {
msg string msg string
code ErrorCode typ ErrorType
slug string code string
} }
var _ Error = simpleError{} var _ Error = simpleError{}
@ -44,10 +44,10 @@ func (e simpleError) Error() string {
return e.msg return e.msg
} }
func (e simpleError) Code() ErrorCode { func (e simpleError) Type() ErrorType {
return e.code return e.typ
} }
func (e simpleError) Slug() string { func (e simpleError) Code() string {
return e.slug return e.code
} }

View File

@ -625,11 +625,11 @@ func (e ServerNotFoundError) Error() string {
return fmt.Sprintf("server with key %s not found", e.Key) return fmt.Sprintf("server with key %s not found", e.Key)
} }
func (e ServerNotFoundError) Code() ErrorCode { func (e ServerNotFoundError) Type() ErrorType {
return ErrorCodeNotFound return ErrorTypeNotFound
} }
func (e ServerNotFoundError) Slug() string { func (e ServerNotFoundError) Code() string {
return "server-not-found" return "server-not-found"
} }

View File

@ -37,19 +37,19 @@ func (e ValidationError) Path() string {
return path 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 var domainErr Error
if errors.As(e.Err, &domainErr) { if errors.As(e.Err, &domainErr) {
return domainErr.Code() 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" return "validation-failed"
} }
@ -97,19 +97,19 @@ func (e SliceElementValidationError) Path() string {
return path 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 var domainErr Error
if errors.As(e.Err, &domainErr) { if errors.As(e.Err, &domainErr) {
return domainErr.Code() 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" 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) return fmt.Sprintf("must be no less than %d (current: %d)", e.Min, e.Current)
} }
func (e MinGreaterEqualError) Code() ErrorCode { func (e MinGreaterEqualError) Type() ErrorType {
return ErrorCodeIncorrectInput return ErrorTypeIncorrectInput
} }
func (e MinGreaterEqualError) Slug() string { func (e MinGreaterEqualError) Code() string {
return "min-greater-equal" 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) return fmt.Sprintf("must be no greater than %d (current: %d)", e.Max, e.Current)
} }
func (e MaxLessEqualError) Code() ErrorCode { func (e MaxLessEqualError) Type() ErrorType {
return ErrorCodeIncorrectInput return ErrorTypeIncorrectInput
} }
func (e MaxLessEqualError) Slug() string { func (e MaxLessEqualError) Code() string {
return "max-less-equal" 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) return fmt.Sprintf("length must be between %d and %d (current length: %d)", e.Min, e.Max, e.Current)
} }
func (e LenOutOfRangeError) Code() ErrorCode { func (e LenOutOfRangeError) Type() ErrorType {
return ErrorCodeIncorrectInput return ErrorTypeIncorrectInput
} }
func (e LenOutOfRangeError) Slug() string { func (e LenOutOfRangeError) Code() string {
return "length-out-of-range" return "length-out-of-range"
} }
@ -215,11 +215,11 @@ func (e InvalidURLError) Error() string {
return fmt.Sprintf("%s: invalid URL", e.URL) return fmt.Sprintf("%s: invalid URL", e.URL)
} }
func (e InvalidURLError) Code() ErrorCode { func (e InvalidURLError) Type() ErrorType {
return ErrorCodeIncorrectInput return ErrorTypeIncorrectInput
} }
func (e InvalidURLError) Slug() string { func (e InvalidURLError) Code() string {
return "invalid-url" return "invalid-url"
} }
@ -231,20 +231,20 @@ func (e InvalidURLError) Params() map[string]any {
var ErrRequired error = simpleError{ var ErrRequired error = simpleError{
msg: "can't be blank", msg: "can't be blank",
code: ErrorCodeIncorrectInput, typ: ErrorTypeIncorrectInput,
slug: "required", code: "required",
} }
var ErrNil error = simpleError{ var ErrNil error = simpleError{
msg: "must not be nil", msg: "must not be nil",
code: ErrorCodeIncorrectInput, typ: ErrorTypeIncorrectInput,
slug: "nil", code: "nil",
} }
var ErrInvalidCursor error = simpleError{ var ErrInvalidCursor error = simpleError{
msg: "invalid cursor", msg: "invalid cursor",
code: ErrorCodeIncorrectInput, typ: ErrorTypeIncorrectInput,
slug: "invalid-cursor", code: "invalid-cursor",
} }
func validateSliceLen[S ~[]E, E any](s S, min, max int) error { func validateSliceLen[S ~[]E, E any](s S, min, max int) error {

View File

@ -328,11 +328,11 @@ func (e VersionNotFoundError) Error() string {
return fmt.Sprintf("version with code %s not found", e.VersionCode) return fmt.Sprintf("version with code %s not found", e.VersionCode)
} }
func (e VersionNotFoundError) Code() ErrorCode { func (e VersionNotFoundError) Type() ErrorType {
return ErrorCodeNotFound return ErrorTypeNotFound
} }
func (e VersionNotFoundError) Slug() string { func (e VersionNotFoundError) Code() string {
return "version-not-found" return "version-not-found"
} }

View File

@ -69,7 +69,7 @@ func (h *apiHTTPHandler) handleNotFound(w http.ResponseWriter, r *http.Request)
errors: []error{ errors: []error{
apiError{ apiError{
status: http.StatusNotFound, status: http.StatusNotFound,
slug: "route-not-found", code: "route-not-found",
message: "route not found", message: "route not found",
}, },
}, },
@ -81,7 +81,7 @@ func (h *apiHTTPHandler) handleMethodNotAllowed(w http.ResponseWriter, r *http.R
errors: []error{ errors: []error{
apiError{ apiError{
status: http.StatusMethodNotAllowed, status: http.StatusMethodNotAllowed,
slug: "method-not-allowed", code: "method-not-allowed",
message: "method not allowed", message: "method not allowed",
}, },
}, },

View File

@ -11,7 +11,7 @@ import (
type apiError struct { type apiError struct {
status int status int
slug string code string
path []string path []string
params map[string]any params map[string]any
message string message string
@ -28,7 +28,7 @@ func (e apiError) toResponse() apimodel.ErrorResponse {
Message: e.message, Message: e.message,
Params: e.params, Params: e.params,
Path: e.path, Path: e.path,
Slug: e.slug, Code: e.code,
}, },
}, },
} }
@ -80,7 +80,7 @@ type apiErrorRenderer struct {
var errInternalServerError = apiError{ var errInternalServerError = apiError{
status: http.StatusInternalServerError, status: http.StatusInternalServerError,
slug: "internal-server-error", code: "internal-server-error",
message: "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, &paramFormatErr): case errors.As(err, &paramFormatErr):
apiErr = apiError{ apiErr = apiError{
status: http.StatusBadRequest, status: http.StatusBadRequest,
slug: "invalid-param-format", code: "invalid-param-format",
path: []string{"$query", paramFormatErr.ParamName}, path: []string{"$query", paramFormatErr.ParamName},
message: paramFormatErr.Err.Error(), message: paramFormatErr.Err.Error(),
} }
@ -169,21 +169,21 @@ func (re apiErrorRenderer) domainErrorToAPIError(domainErr domain.Error) apiErro
} }
return apiError{ return apiError{
status: errorCodeToStatusCode(domainErr.Code()), status: errorTypeToStatusCode(domainErr.Type()),
slug: domainErr.Slug(), code: domainErr.Code(),
path: path, path: path,
params: cloned, params: cloned,
message: message, message: message,
} }
} }
func errorCodeToStatusCode(code domain.ErrorCode) int { func errorTypeToStatusCode(code domain.ErrorType) int {
switch code { switch code {
case domain.ErrorCodeIncorrectInput: case domain.ErrorTypeIncorrectInput:
return http.StatusBadRequest return http.StatusBadRequest
case domain.ErrorCodeNotFound: case domain.ErrorTypeNotFound:
return http.StatusNotFound return http.StatusNotFound
case domain.ErrorCodeUnknown: case domain.ErrorTypeUnknown:
fallthrough fallthrough
default: default:
return http.StatusInternalServerError return http.StatusInternalServerError