crowdsec/pkg/apiserver/controllers/v1/errors.go
Thibault "bui" Koechlin 71ac0d2fce
Apiclient tests (#484)
Co-authored-by: AlteredCoder
Co-authored-by: erenJag
2020-11-30 16:15:07 +01:00

39 lines
1.1 KiB
Go

package v1
import (
"net/http"
"github.com/crowdsecurity/crowdsec/pkg/database"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
func (c *Controller) HandleDBErrors(gctx *gin.Context, err error) {
switch errors.Cause(err) {
case database.ItemNotFound:
gctx.JSON(http.StatusNotFound, gin.H{"message": err.Error()})
return
case database.UserExists:
gctx.JSON(http.StatusForbidden, gin.H{"message": err.Error()})
return
case database.HashError:
gctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
case database.InsertFail:
gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
case database.QueryFail:
gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
case database.ParseTimeFail:
gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
case database.ParseDurationFail:
gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
default:
gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}
}