backend moved to separate directory
backend: deleted the old structure
This commit is contained in:
50
go-backend/internal/utils/response_handler.go
Normal file
50
go-backend/internal/utils/response_handler.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"GoMembership/pkg/errors"
|
||||
"GoMembership/pkg/logger"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func RespondWithError(c *gin.Context, err error, context string, code int, field string, key string) {
|
||||
logger.Error.Printf("Sending %v Error Response(Field: %v Key: %v) %v: %v", code, field, key, context, err.Error())
|
||||
c.JSON(code, gin.H{"errors": []gin.H{{
|
||||
"field": field,
|
||||
"key": key,
|
||||
}}})
|
||||
}
|
||||
|
||||
func HandleValidationError(c *gin.Context, err error) {
|
||||
var validationErrors []gin.H
|
||||
logger.Error.Printf("Sending validation error response Error %v", err.Error())
|
||||
if ve, ok := err.(validator.ValidationErrors); ok {
|
||||
for _, e := range ve {
|
||||
validationErrors = append(validationErrors, gin.H{
|
||||
"field": e.Field(),
|
||||
"key": "server.validation." + e.Tag(),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
validationErrors = append(validationErrors, gin.H{
|
||||
"field": "general",
|
||||
"key": "server.error.invalid_json",
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errors": validationErrors})
|
||||
}
|
||||
|
||||
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")
|
||||
case errors.ErrSubscriptionNotFound:
|
||||
RespondWithError(c, err, "Error while updating user", http.StatusBadRequest, "subscription", "server.validation.subscription_data")
|
||||
default:
|
||||
RespondWithError(c, err, "Error while updating user", http.StatusInternalServerError, "user.user", "server.error.internal_server_error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user