error handling

This commit is contained in:
Alex
2025-02-28 10:46:40 +01:00
parent b0271f8443
commit a2e8abbf6b
6 changed files with 59 additions and 38 deletions

View File

@@ -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(&regData.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
}