package services import ( "actatempus_backend/internal/domain/app_error" "net/http" "github.com/gin-gonic/gin" ) // HandleError handles errors by formatting them as JSON. func HandleError(c *gin.Context) func(error) any { return func(err error) any { // Check if the error is of type *AppError if appErr, ok := err.(*app_error.AppError); ok { // Use the AppError fields for the JSON response c.JSON(appErr.Status, gin.H{ "code": appErr.Code, "message": appErr.Message, "details": appErr.Err.Error(), // Original error if available }) } else { // Fallback for non-AppError errors c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), }) } return nil } } func HandleSuccess[T any](c *gin.Context, statusCode int) func(T) any { return func(data T) any { c.JSON(statusCode, data) return nil } }