You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.6 KiB

  1. package api
  2. import (
  3. "database/sql"
  4. "errors"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. "github.com/hermeznetwork/hermez-node/db/historydb"
  8. "github.com/hermeznetwork/hermez-node/log"
  9. "github.com/hermeznetwork/tracerr"
  10. "github.com/lib/pq"
  11. )
  12. const (
  13. // maxLimit is the max permited items to be returned in paginated responses
  14. maxLimit uint = 2049
  15. // dfltOrder indicates how paginated endpoints are ordered if not specified
  16. dfltOrder = historydb.OrderAsc
  17. // dfltLimit indicates the limit of returned items in paginated responses if the query param limit is not provided
  18. dfltLimit uint = 20
  19. // 2^32 -1
  20. maxUint32 = 4294967295
  21. // 2^64 /2 -1
  22. maxInt64 = 9223372036854775807
  23. // Error for duplicated key
  24. errDuplicatedKey = "Item already exists"
  25. )
  26. var (
  27. // ErrNillBidderAddr is used when a nil bidderAddr is received in the getCoordinator method
  28. ErrNillBidderAddr = errors.New("biderAddr can not be nil")
  29. )
  30. func retSQLErr(err error, c *gin.Context) {
  31. log.Warn("HTTP API SQL request error", "err", err)
  32. if sqlErr, ok := tracerr.Unwrap(err).(*pq.Error); ok {
  33. // https://www.postgresql.org/docs/current/errcodes-appendix.html
  34. if sqlErr.Code == "23505" {
  35. c.JSON(http.StatusInternalServerError, errorMsg{
  36. Message: errDuplicatedKey,
  37. })
  38. }
  39. }
  40. if tracerr.Unwrap(err) == sql.ErrNoRows {
  41. c.JSON(http.StatusNotFound, errorMsg{
  42. Message: err.Error(),
  43. })
  44. } else {
  45. c.JSON(http.StatusInternalServerError, errorMsg{
  46. Message: err.Error(),
  47. })
  48. }
  49. }
  50. func retBadReq(err error, c *gin.Context) {
  51. log.Warn("HTTP API Bad request error", "err", err)
  52. c.JSON(http.StatusBadRequest, errorMsg{
  53. Message: err.Error(),
  54. })
  55. }