Commit 53822884 by mhmdhaekal

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

1 parent 603f30f0
......@@ -19,6 +19,7 @@ var (
Validation = ErrorCode{"validation_error"}
Database = ErrorCode{"database_error"}
InactiveUser = ErrorCode{"user_inactive"}
Unauthorized = ErrorCode{"unauthorized"}
)
func FromString(s string) (ErrorCode, error) {
......@@ -69,6 +70,10 @@ var (
Code: InactiveUser,
Message: "user account is inactive",
}
ErrUnauthorized = &Error{
Code: Unauthorized,
Message: "Unauthorized",
}
)
func NewError(code ErrorCode, message string, err error) *Error {
......
......@@ -13,7 +13,16 @@ func ErrorHandler(ctx fiber.Ctx, err error) error {
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 {
case domain.NotFound:
code = fiber.StatusNotFound
......@@ -23,11 +32,11 @@ func ErrorHandler(ctx fiber.Ctx, err error) error {
code = fiber.StatusInternalServerError
case domain.InactiveUser:
code = fiber.StatusForbidden
case domain.Unauthorized:
code = fiber.StatusUnauthorized
default:
code = fiber.StatusInternalServerError
}
} else if fiberErr, ok := err.(*fiber.Error); ok {
code = fiberErr.Code
}
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!