fix: check if Error is null in json response

This commit is contained in:
Jean Jacques Avril 2025-01-03 19:16:15 +00:00
parent 8e6c578dbd
commit 03c5908605
No known key found for this signature in database

View File

@ -12,12 +12,18 @@ func HandleError(c *gin.Context) func(error) any {
return func(err error) any { return func(err error) any {
// Check if the error is of type *AppError // Check if the error is of type *AppError
if appErr, ok := err.(*app_error.AppError); ok { if appErr, ok := err.(*app_error.AppError); ok {
// Use the AppError fields for the JSON response response := gin.H{
c.JSON(appErr.Status, gin.H{
"code": appErr.Code, "code": appErr.Code,
"message": appErr.Message, "message": appErr.Message,
"details": appErr.Err.Error(), // Original error if available }
})
// 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 { } else {
// Fallback for non-AppError errors // Fallback for non-AppError errors
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{