From a2e8abbf6b5eec7cd69e03eb6138362cef8a88bd Mon Sep 17 00:00:00 2001 From: Alex <$(pass /github/email)> Date: Fri, 28 Feb 2025 10:46:40 +0100 Subject: [PATCH] error handling --- .../internal/controllers/licenceController.go | 3 +- .../controllers/membershipController.go | 22 +------- .../internal/controllers/user_Password.go | 13 ++--- .../internal/controllers/user_controller.go | 3 +- go-backend/internal/utils/response_handler.go | 54 +++++++++++++++++-- go-backend/pkg/errors/errors.go | 2 + 6 files changed, 59 insertions(+), 38 deletions(-) diff --git a/go-backend/internal/controllers/licenceController.go b/go-backend/internal/controllers/licenceController.go index a011a85..ebf1c96 100644 --- a/go-backend/internal/controllers/licenceController.go +++ b/go-backend/internal/controllers/licenceController.go @@ -3,6 +3,7 @@ package controllers import ( "GoMembership/internal/services" "GoMembership/internal/utils" + "GoMembership/pkg/errors" "net/http" "github.com/gin-gonic/gin" @@ -17,7 +18,7 @@ func (lc *LicenceController) GetAllCategories(c *gin.Context) { categories, err := lc.Service.GetAllCategories() if err != nil { - utils.RespondWithError(c, err, "Error retrieving licence categories", http.StatusInternalServerError, "general", "server.error.internal_server_error") + utils.RespondWithError(c, err, "Error retrieving licence categories", http.StatusInternalServerError, errors.Responses.Fields.Licences, errors.Responses.Keys.InternalServerError) return } c.JSON(http.StatusOK, gin.H{ diff --git a/go-backend/internal/controllers/membershipController.go b/go-backend/internal/controllers/membershipController.go index 75d6147..36a4f98 100644 --- a/go-backend/internal/controllers/membershipController.go +++ b/go-backend/internal/controllers/membershipController.go @@ -86,15 +86,7 @@ func (mc *MembershipController) UpdateHandler(c *gin.Context) { logger.Info.Printf("Updating subscription %v", regData.Subscription.Name) id, err := mc.Service.UpdateSubscription(®Data.Subscription) if err != nil { - if strings.Contains(err.Error(), "UNIQUE constraint failed") { - utils.RespondWithError(c, err, "Subscription already exists", http.StatusConflict, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Duplicate) - } else if err == errors.ErrSubscriptionNotFound { - utils.RespondWithError(c, err, "Subscription not found", http.StatusNotFound, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.NotFound) - } else if err == errors.ErrInvalidSubscriptionData { - utils.RespondWithError(c, err, "Invalid subscription data", http.StatusBadRequest, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) - } else { - utils.RespondWithError(c, err, "Couldn't update subscription", http.StatusInternalServerError, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.InternalServerError) - } + utils.HandleSubscriptionUpdateError(c, err) return } logger.Info.Printf("updating subscription: %+v", regData) @@ -130,17 +122,7 @@ func (mc *MembershipController) DeleteSubscription(c *gin.Context) { } if err := mc.Service.DeleteSubscription(&data.Subscription.ID, &data.Subscription.Name); err != nil { - if err == errors.ErrNoData { - utils.RespondWithError(c, err, "Missing subscription name during deletion", http.StatusExpectationFailed, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) - } else if err == errors.ErrSubscriptionNotFound { - utils.RespondWithError(c, err, "Subscription not found", http.StatusNotFound, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.NotFound) - } else if err == errors.ErrInvalidSubscriptionData { - utils.RespondWithError(c, err, "Invalid subscription data", http.StatusBadRequest, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) - } else if err == errors.ErrSubscriptionInUse { - utils.RespondWithError(c, err, "Subscription is in use by at least one user", http.StatusExpectationFailed, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.InUse) - } else { - utils.RespondWithError(c, err, "Error during subscription Deletion", http.StatusInternalServerError, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.InternalServerError) - } + utils.HandleSubscriptionDeleteError(c, err) return } diff --git a/go-backend/internal/controllers/user_Password.go b/go-backend/internal/controllers/user_Password.go index 8160688..4e3a0fd 100644 --- a/go-backend/internal/controllers/user_Password.go +++ b/go-backend/internal/controllers/user_Password.go @@ -24,7 +24,7 @@ func (uc *UserController) RequestPasswordChangeHandler(c *gin.Context) { // find user db_user, err := uc.Service.GetUserByEmail(input.Email) if err != nil { - utils.RespondWithError(c, err, "couldn't get user by email", http.StatusNotFound, "user.user", "user.email") + utils.RespondWithError(c, err, "couldn't get user by email", http.StatusNotFound, errors.Responses.Fields.User, errors.Responses.Keys.NotFound) return } @@ -71,14 +71,7 @@ func (uc *UserController) ChangePassword(c *gin.Context) { verification, err := uc.Service.VerifyUser(&input.Token, &constants.VerificationTypes.Password) if err != nil || uint(userIDint) != verification.UserID { - if err == errors.ErrAlreadyVerified { - utils.RespondWithError(c, err, "User already changed password", http.StatusConflict, errors.Responses.Fields.User, errors.Responses.Keys.PasswordAlreadyChanged) - } else if err.Error() == "record not found" { - utils.RespondWithError(c, err, "Couldn't find verification. This is most probably a outdated token.", http.StatusGone, errors.Responses.Fields.User, errors.Responses.Keys.NoAuthToken) - } else { - utils.RespondWithError(c, err, "Couldn't verify user", http.StatusInternalServerError, errors.Responses.Fields.General, errors.Responses.Keys.InternalServerError) - } - + utils.HandleVerifyUserError(c, err) return } @@ -95,7 +88,7 @@ func (uc *UserController) ChangePassword(c *gin.Context) { _, err = uc.Service.UpdateUser(user) if err != nil { - utils.RespondWithError(c, err, "Couldn't update user", http.StatusInternalServerError, errors.Responses.Fields.User, errors.Responses.Keys.InternalServerError) + utils.HandleUserUpdateError(c, err) return } diff --git a/go-backend/internal/controllers/user_controller.go b/go-backend/internal/controllers/user_controller.go index 304f9fb..0bd41ad 100644 --- a/go-backend/internal/controllers/user_controller.go +++ b/go-backend/internal/controllers/user_controller.go @@ -342,8 +342,7 @@ func (uc *UserController) VerifyMailHandler(c *gin.Context) { verification, err := uc.Service.VerifyUser(&token, &constants.VerificationTypes.Email) if err != nil { - logger.Error.Printf("Cannot verify user: %v", err) - c.HTML(http.StatusUnauthorized, "verification_error.html", gin.H{"ErrorMessage": "Emailadresse wurde schon bestÃĪtigt. Sollte dies nicht der Fall sein, wende Dich bitte an info@carsharing-hasloh.de."}) + utils.HandleVerifyUserError(c, err) return } diff --git a/go-backend/internal/utils/response_handler.go b/go-backend/internal/utils/response_handler.go index e33dcce..02be255 100644 --- a/go-backend/internal/utils/response_handler.go +++ b/go-backend/internal/utils/response_handler.go @@ -4,6 +4,7 @@ import ( "GoMembership/pkg/errors" "GoMembership/pkg/logger" "net/http" + "strings" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" @@ -39,12 +40,55 @@ func HandleValidationError(c *gin.Context, err error) { func HandleUserUpdateError(c *gin.Context, err error) { switch err { case errors.ErrUserNotFound: - RespondWithError(c, err, "Error while updating user", http.StatusNotFound, "user.user", "server.validation.user_not_found") - case errors.ErrInvalidUserData: - RespondWithError(c, err, "Error while updating user", http.StatusBadRequest, "user.user", "server.validation.invalid_user_data") + RespondWithError(c, err, "Couldn't find user", http.StatusNotFound, errors.Responses.Fields.User, errors.Responses.Keys.NotFound) + case errors.ErrDuplicateEntry: + RespondWithError(c, err, "User Unique constraint failed", http.StatusConflict, errors.Responses.Fields.User, errors.Responses.Keys.Duplicate) case errors.ErrSubscriptionNotFound: - RespondWithError(c, err, "Error while updating user", http.StatusBadRequest, "subscription", "server.validation.subscription_data") + RespondWithError(c, err, "Couldn't find subscription", http.StatusNotFound, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.NotFound) default: - RespondWithError(c, err, "Error while updating user", http.StatusInternalServerError, "user.user", "server.error.internal_server_error") + } + RespondWithError(c, err, "Couldn't update user", http.StatusInternalServerError, errors.Responses.Fields.User, errors.Responses.Keys.InternalServerError) +} + +func HandleSubscriptionDeleteError(c *gin.Context, err error) { + switch err { + case errors.ErrNoData: + RespondWithError(c, err, "Missing subscription name during deletion", http.StatusExpectationFailed, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) + case errors.ErrSubscriptionNotFound: + RespondWithError(c, err, "Subscription not found", http.StatusNotFound, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.NotFound) + case errors.ErrInvalidSubscriptionData: + RespondWithError(c, err, "Invalid subscription data", http.StatusBadRequest, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) + case errors.ErrSubscriptionInUse: + RespondWithError(c, err, "Subscription is in use by at least one user", http.StatusExpectationFailed, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.InUse) + default: + RespondWithError(c, err, "Error during subscription Deletion", http.StatusInternalServerError, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.InternalServerError) + } +} + +func HandleSubscriptionUpdateError(c *gin.Context, err error) { + + if strings.Contains(err.Error(), "UNIQUE constraint failed") { + RespondWithError(c, err, "Subscription already exists", http.StatusConflict, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Duplicate) + } else { + switch err { + case errors.ErrSubscriptionNotFound: + RespondWithError(c, err, "Subscription not found", http.StatusNotFound, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.NotFound) + case errors.ErrInvalidSubscriptionData: + RespondWithError(c, err, "Invalid subscription data", http.StatusBadRequest, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) + default: + RespondWithError(c, err, "Couldn't update subscription", http.StatusInternalServerError, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.InternalServerError) + } + } +} + +func HandleVerifyUserError(c *gin.Context, err error) { + if err.Error() == "record not found" { + RespondWithError(c, err, "Couldn't find verification. This is most probably a outdated token.", http.StatusGone, errors.Responses.Fields.User, errors.Responses.Keys.NoAuthToken) + } + switch err { + case errors.ErrAlreadyVerified: + RespondWithError(c, err, "User already changed password", http.StatusConflict, errors.Responses.Fields.User, errors.Responses.Keys.PasswordAlreadyChanged) + default: + RespondWithError(c, err, "Couldn't verify user", http.StatusInternalServerError, errors.Responses.Fields.General, errors.Responses.Keys.InternalServerError) } } diff --git a/go-backend/pkg/errors/errors.go b/go-backend/pkg/errors/errors.go index 72b404b..b7fc3a2 100644 --- a/go-backend/pkg/errors/errors.go +++ b/go-backend/pkg/errors/errors.go @@ -26,6 +26,7 @@ type ValidationFields struct { Login string Email string User string + Licences string } var ( @@ -77,5 +78,6 @@ var Responses = struct { Login: "user.login", Email: "user.email", User: "user.user", + Licences: "licence", }, }