errors.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package v1
  2. import (
  3. "net/http"
  4. "github.com/crowdsecurity/crowdsec/pkg/database"
  5. "github.com/gin-gonic/gin"
  6. "github.com/pkg/errors"
  7. )
  8. func (c *Controller) HandleDBErrors(gctx *gin.Context, err error) {
  9. switch errors.Cause(err) {
  10. case database.ItemNotFound:
  11. gctx.JSON(http.StatusNotFound, gin.H{"message": err.Error()})
  12. return
  13. case database.UserExists:
  14. gctx.JSON(http.StatusForbidden, gin.H{"message": err.Error()})
  15. return
  16. case database.HashError:
  17. gctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
  18. return
  19. case database.InsertFail:
  20. gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
  21. return
  22. case database.QueryFail:
  23. gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
  24. return
  25. case database.ParseTimeFail:
  26. gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
  27. return
  28. case database.ParseDurationFail:
  29. gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
  30. return
  31. default:
  32. gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
  33. return
  34. }
  35. }