VILLASweb-backend-go/helper/http_errors.go
Sonja Happ f3a7ed0e61 **Major revision of repository structure**
- rename common package to database
- move all code not related to database to new helper package
- add more test in database package to improve code coverage
- add a new (own) package for AMQP client
2019-09-10 16:28:57 +02:00

69 lines
1.5 KiB
Go

package helper
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"net/http"
)
func DBError(c *gin.Context, err error) bool {
if err != nil {
if err == gorm.ErrRecordNotFound {
NotFoundError(c, "Record not Found in DB: "+err.Error())
} else {
InternalServerError(c, "Error on DB Query or transaction: "+err.Error())
}
return true // Error
}
return false // No error
}
func BadRequestError(c *gin.Context, err string) {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": fmt.Sprintf("%v", err),
})
}
func UnprocessableEntityError(c *gin.Context, err string) {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"success": false,
"message": fmt.Sprintf("%v", err),
})
}
func InternalServerError(c *gin.Context, err string) {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": fmt.Sprintf("%v", err),
})
}
func UnauthorizedError(c *gin.Context, err string) {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": fmt.Sprintf("%v", err),
})
}
func UnauthorizedAbort(c *gin.Context, err string) {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"succes": false,
"message": fmt.Sprintf("%v", err),
})
}
func NotFoundError(c *gin.Context, err string) {
c.JSON(http.StatusNotFound, gin.H{
"success": false,
"message": fmt.Sprintf("%v", err),
})
}
func ForbiddenError(c *gin.Context, err string) {
c.JSON(http.StatusForbidden, gin.H{
"success": false,
"message": fmt.Sprintf("%v", err),
})
}