44 lines
935 B
Go
44 lines
935 B
Go
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 {
|
|
response := gin.H{
|
|
"code": appErr.Code,
|
|
"message": appErr.Message,
|
|
}
|
|
|
|
// Add details if available
|
|
if appErr.Err != nil {
|
|
response["details"] = appErr.Err.Error()
|
|
}
|
|
|
|
// Use the AppError fields for the JSON response
|
|
c.JSON(appErr.Status, response)
|
|
} 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
|
|
}
|
|
}
|