error handling
This commit is contained in:
@@ -3,6 +3,7 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"GoMembership/internal/services"
|
"GoMembership/internal/services"
|
||||||
"GoMembership/internal/utils"
|
"GoMembership/internal/utils"
|
||||||
|
"GoMembership/pkg/errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -17,7 +18,7 @@ func (lc *LicenceController) GetAllCategories(c *gin.Context) {
|
|||||||
categories, err := lc.Service.GetAllCategories()
|
categories, err := lc.Service.GetAllCategories()
|
||||||
|
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
|||||||
@@ -86,15 +86,7 @@ func (mc *MembershipController) UpdateHandler(c *gin.Context) {
|
|||||||
logger.Info.Printf("Updating subscription %v", regData.Subscription.Name)
|
logger.Info.Printf("Updating subscription %v", regData.Subscription.Name)
|
||||||
id, err := mc.Service.UpdateSubscription(®Data.Subscription)
|
id, err := mc.Service.UpdateSubscription(®Data.Subscription)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
|
utils.HandleSubscriptionUpdateError(c, err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Info.Printf("updating subscription: %+v", regData)
|
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 := mc.Service.DeleteSubscription(&data.Subscription.ID, &data.Subscription.Name); err != nil {
|
||||||
if err == errors.ErrNoData {
|
utils.HandleSubscriptionDeleteError(c, err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func (uc *UserController) RequestPasswordChangeHandler(c *gin.Context) {
|
|||||||
// find user
|
// find user
|
||||||
db_user, err := uc.Service.GetUserByEmail(input.Email)
|
db_user, err := uc.Service.GetUserByEmail(input.Email)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,14 +71,7 @@ func (uc *UserController) ChangePassword(c *gin.Context) {
|
|||||||
|
|
||||||
verification, err := uc.Service.VerifyUser(&input.Token, &constants.VerificationTypes.Password)
|
verification, err := uc.Service.VerifyUser(&input.Token, &constants.VerificationTypes.Password)
|
||||||
if err != nil || uint(userIDint) != verification.UserID {
|
if err != nil || uint(userIDint) != verification.UserID {
|
||||||
if err == errors.ErrAlreadyVerified {
|
utils.HandleVerifyUserError(c, err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +88,7 @@ func (uc *UserController) ChangePassword(c *gin.Context) {
|
|||||||
|
|
||||||
_, err = uc.Service.UpdateUser(user)
|
_, err = uc.Service.UpdateUser(user)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -342,8 +342,7 @@ func (uc *UserController) VerifyMailHandler(c *gin.Context) {
|
|||||||
|
|
||||||
verification, err := uc.Service.VerifyUser(&token, &constants.VerificationTypes.Email)
|
verification, err := uc.Service.VerifyUser(&token, &constants.VerificationTypes.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error.Printf("Cannot verify user: %v", err)
|
utils.HandleVerifyUserError(c, 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."})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"GoMembership/pkg/errors"
|
"GoMembership/pkg/errors"
|
||||||
"GoMembership/pkg/logger"
|
"GoMembership/pkg/logger"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
@@ -39,12 +40,55 @@ func HandleValidationError(c *gin.Context, err error) {
|
|||||||
func HandleUserUpdateError(c *gin.Context, err error) {
|
func HandleUserUpdateError(c *gin.Context, err error) {
|
||||||
switch err {
|
switch err {
|
||||||
case errors.ErrUserNotFound:
|
case errors.ErrUserNotFound:
|
||||||
RespondWithError(c, err, "Error while updating user", http.StatusNotFound, "user.user", "server.validation.user_not_found")
|
RespondWithError(c, err, "Couldn't find user", http.StatusNotFound, errors.Responses.Fields.User, errors.Responses.Keys.NotFound)
|
||||||
case errors.ErrInvalidUserData:
|
case errors.ErrDuplicateEntry:
|
||||||
RespondWithError(c, err, "Error while updating user", http.StatusBadRequest, "user.user", "server.validation.invalid_user_data")
|
RespondWithError(c, err, "User Unique constraint failed", http.StatusConflict, errors.Responses.Fields.User, errors.Responses.Keys.Duplicate)
|
||||||
case errors.ErrSubscriptionNotFound:
|
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:
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ type ValidationFields struct {
|
|||||||
Login string
|
Login string
|
||||||
Email string
|
Email string
|
||||||
User string
|
User string
|
||||||
|
Licences string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -77,5 +78,6 @@ var Responses = struct {
|
|||||||
Login: "user.login",
|
Login: "user.login",
|
||||||
Email: "user.email",
|
Email: "user.email",
|
||||||
User: "user.user",
|
User: "user.user",
|
||||||
|
Licences: "licence",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user