Files
pocket-id-pocket-id-2/backend/internal/utils/handler_error_util.go

28 lines
611 B
Go
Raw Normal View History

2024-08-12 11:00:25 +02:00
package utils
import (
"errors"
2024-08-12 11:00:25 +02:00
"github.com/gin-gonic/gin"
"gorm.io/gorm"
2024-08-12 11:00:25 +02:00
"log"
"net/http"
"strings"
)
func UnknownHandlerError(c *gin.Context, err error) {
if errors.Is(err, gorm.ErrRecordNotFound) {
HandlerError(c, http.StatusNotFound, "Record not found")
return
} else {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Something went wrong"})
}
2024-08-12 11:00:25 +02:00
}
func HandlerError(c *gin.Context, statusCode int, message string) {
// Capitalize the first letter of the message
message = strings.ToUpper(message[:1]) + message[1:]
c.JSON(statusCode, gin.H{"error": message})
}