56 lines
2.7 KiB
Go
56 lines
2.7 KiB
Go
|
|
package errorx
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"img_infinite_canvas/internal/domain/design"
|
||
|
|
authmodule "img_infinite_canvas/internal/modules/auth"
|
||
|
|
)
|
||
|
|
|
||
|
|
type CodeError struct {
|
||
|
|
Code int `json:"code"`
|
||
|
|
Message string `json:"message"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (e *CodeError) Error() string {
|
||
|
|
return e.Message
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewCodeError(code int, message string) error {
|
||
|
|
return &CodeError{Code: code, Message: message}
|
||
|
|
}
|
||
|
|
|
||
|
|
func Handle(ctx context.Context, err error) (int, any) {
|
||
|
|
var codeErr *CodeError
|
||
|
|
switch {
|
||
|
|
case errors.As(err, &codeErr):
|
||
|
|
return codeErr.Code, map[string]any{"error": codeErr.Message, "code": codeErr.Code}
|
||
|
|
case errors.Is(err, design.ErrNotFound):
|
||
|
|
return http.StatusNotFound, map[string]any{"error": design.ErrNotFound.Error(), "code": http.StatusNotFound}
|
||
|
|
case errors.Is(err, design.ErrInvalidInput):
|
||
|
|
return http.StatusBadRequest, map[string]any{"error": err.Error(), "code": http.StatusBadRequest}
|
||
|
|
case errors.Is(err, design.ErrConflict):
|
||
|
|
return http.StatusConflict, map[string]any{"error": err.Error(), "code": http.StatusConflict}
|
||
|
|
case errors.Is(err, authmodule.ErrCodeInvalid):
|
||
|
|
return http.StatusUnauthorized, map[string]any{"error": err.Error(), "errorCode": "auth.code_invalid", "code": http.StatusUnauthorized}
|
||
|
|
case errors.Is(err, authmodule.ErrCodeExpired):
|
||
|
|
return http.StatusUnauthorized, map[string]any{"error": err.Error(), "errorCode": "auth.code_expired", "code": http.StatusUnauthorized}
|
||
|
|
case errors.Is(err, authmodule.ErrInvalidCredentials):
|
||
|
|
return http.StatusUnauthorized, map[string]any{"error": err.Error(), "errorCode": "auth.invalid_credentials", "code": http.StatusUnauthorized}
|
||
|
|
case errors.Is(err, authmodule.ErrInvalidRegion):
|
||
|
|
return http.StatusBadRequest, map[string]any{"error": err.Error(), "errorCode": "auth.invalid_region", "code": http.StatusBadRequest}
|
||
|
|
case errors.Is(err, authmodule.ErrHumanVerificationRequired):
|
||
|
|
return http.StatusBadRequest, map[string]any{"error": err.Error(), "errorCode": "auth.human_verification_required", "code": http.StatusBadRequest}
|
||
|
|
case errors.Is(err, authmodule.ErrHumanVerificationInvalid):
|
||
|
|
return http.StatusBadRequest, map[string]any{"error": err.Error(), "errorCode": "auth.human_verification_failed", "code": http.StatusBadRequest}
|
||
|
|
case errors.Is(err, authmodule.ErrProviderNotReady):
|
||
|
|
return http.StatusBadRequest, map[string]any{"error": err.Error(), "errorCode": "auth.provider_not_ready", "code": http.StatusBadRequest}
|
||
|
|
case errors.Is(err, context.Canceled):
|
||
|
|
return http.StatusRequestTimeout, map[string]any{"error": "request canceled", "code": http.StatusRequestTimeout}
|
||
|
|
default:
|
||
|
|
return http.StatusInternalServerError, map[string]any{"error": err.Error(), "code": http.StatusInternalServerError}
|
||
|
|
}
|
||
|
|
}
|