Commit 53822884 by mhmdhaekal

fix(initialize): use default handler if it's gorm error

1 parent 603f30f0
...@@ -19,6 +19,7 @@ var ( ...@@ -19,6 +19,7 @@ var (
Validation = ErrorCode{"validation_error"} Validation = ErrorCode{"validation_error"}
Database = ErrorCode{"database_error"} Database = ErrorCode{"database_error"}
InactiveUser = ErrorCode{"user_inactive"} InactiveUser = ErrorCode{"user_inactive"}
Unauthorized = ErrorCode{"unauthorized"}
) )
func FromString(s string) (ErrorCode, error) { func FromString(s string) (ErrorCode, error) {
...@@ -69,6 +70,10 @@ var ( ...@@ -69,6 +70,10 @@ var (
Code: InactiveUser, Code: InactiveUser,
Message: "user account is inactive", Message: "user account is inactive",
} }
ErrUnauthorized = &Error{
Code: Unauthorized,
Message: "Unauthorized",
}
) )
func NewError(code ErrorCode, message string, err error) *Error { func NewError(code ErrorCode, message string, err error) *Error {
......
...@@ -13,7 +13,16 @@ func ErrorHandler(ctx fiber.Ctx, err error) error { ...@@ -13,7 +13,16 @@ func ErrorHandler(ctx fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError code := fiber.StatusInternalServerError
if customErr, ok := err.(*domain.Error); ok { var fiberErr *fiber.Error
if errors.As(err, &fiberErr) {
code = fiberErr.Code
response := NewBaseResponse(code, nil, err)
return ctx.Status(code).JSON(response)
}
var customErr *domain.Error
if errors.As(err, &customErr) {
switch customErr.Code { switch customErr.Code {
case domain.NotFound: case domain.NotFound:
code = fiber.StatusNotFound code = fiber.StatusNotFound
...@@ -23,11 +32,11 @@ func ErrorHandler(ctx fiber.Ctx, err error) error { ...@@ -23,11 +32,11 @@ func ErrorHandler(ctx fiber.Ctx, err error) error {
code = fiber.StatusInternalServerError code = fiber.StatusInternalServerError
case domain.InactiveUser: case domain.InactiveUser:
code = fiber.StatusForbidden code = fiber.StatusForbidden
case domain.Unauthorized:
code = fiber.StatusUnauthorized
default: default:
code = fiber.StatusInternalServerError code = fiber.StatusInternalServerError
} }
} else if fiberErr, ok := err.(*fiber.Error); ok {
code = fiberErr.Code
} }
response := NewBaseResponse(code, nil, err) response := NewBaseResponse(code, nil, err)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!