From bed94bdf4af9d1f2c3dc176c699d9f928b4cb756 Mon Sep 17 00:00:00 2001 From: Artem Date: Tue, 28 May 2024 10:52:33 +0200 Subject: [PATCH] Fix: cancellation request error --- cmd/api/handler/error.go | 10 ++++++++++ cmd/api/handler/search.go | 12 ++++++------ cmd/api/handler/state.go | 2 +- cmd/api/handler/stats.go | 6 +++--- cmd/api/init.go | 19 ++++++++++--------- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/cmd/api/handler/error.go b/cmd/api/handler/error.go index 5efd3a3..0dea747 100644 --- a/cmd/api/handler/error.go +++ b/cmd/api/handler/error.go @@ -4,6 +4,7 @@ package handler import ( + "context" "net/http" sentryecho "github.com/getsentry/sentry-go/echo" @@ -13,6 +14,7 @@ import ( var ( errInvalidAddress = errors.New("invalid address") + errCancelRequest = "pq: canceling statement due to user request" ) type NoRows interface { @@ -42,6 +44,14 @@ func handleError(c echo.Context, err error, noRows NoRows) error { if err == nil { return nil } + if err.Error() == errCancelRequest { + return nil + } + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + return c.JSON(http.StatusBadGateway, Error{ + Message: err.Error(), + }) + } if noRows.IsNoRows(err) { return c.NoContent(http.StatusNoContent) } diff --git a/cmd/api/handler/search.go b/cmd/api/handler/search.go index 140dcdb..c030543 100644 --- a/cmd/api/handler/search.go +++ b/cmd/api/handler/search.go @@ -59,7 +59,7 @@ func (s *SearchHandler) Search(c echo.Context) error { results, err := s.search.Search(c.Request().Context(), req.Search) if err != nil { - return internalServerError(c, err) + return handleError(c, err, s.address) } response := make([]responses.SearchResult, len(results)) @@ -70,31 +70,31 @@ func (s *SearchHandler) Search(c echo.Context) error { case "block": block, err := s.blocks.GetByID(c.Request().Context(), results[i].Id) if err != nil { - return internalServerError(c, err) + return handleError(c, err, s.address) } body = responses.NewBlock(*block) case "tx": tx, err := s.txs.GetByID(c.Request().Context(), results[i].Id) if err != nil { - return internalServerError(c, err) + return handleError(c, err, s.address) } body = responses.NewTx(*tx) case "rollup": rollup, err := s.rollups.GetByID(c.Request().Context(), results[i].Id) if err != nil { - return internalServerError(c, err) + return handleError(c, err, s.address) } body = responses.NewRollup(rollup) case "address": address, err := s.address.GetByID(c.Request().Context(), results[i].Id) if err != nil { - return internalServerError(c, err) + return handleError(c, err, s.address) } body = responses.NewAddress(*address, nil) case "validator": validator, err := s.validators.GetByID(c.Request().Context(), results[i].Id) if err != nil { - return internalServerError(c, err) + return handleError(c, err, s.address) } body = responses.NewShortValidator(validator) } diff --git a/cmd/api/handler/state.go b/cmd/api/handler/state.go index 6feb35b..4d0fa5a 100644 --- a/cmd/api/handler/state.go +++ b/cmd/api/handler/state.go @@ -37,7 +37,7 @@ func NewStateHandler(state storage.IState) *StateHandler { func (sh *StateHandler) Head(c echo.Context) error { state, err := sh.state.List(c.Request().Context(), 1, 0, sdk.SortOrderAsc) if err != nil { - return internalServerError(c, err) + return handleError(c, err, sh.state) } if len(state) == 0 { return c.NoContent(http.StatusNoContent) diff --git a/cmd/api/handler/stats.go b/cmd/api/handler/stats.go index bf46cb1..bd31ba5 100644 --- a/cmd/api/handler/stats.go +++ b/cmd/api/handler/stats.go @@ -37,7 +37,7 @@ func NewStatsHandler(repo storage.IStats, rollups storage.IRollup) StatsHandler func (sh StatsHandler) Summary(c echo.Context) error { summary, err := sh.repo.Summary(c.Request().Context()) if err != nil { - return internalServerError(c, err) + return handleError(c, err, sh.rollups) } return c.JSON(http.StatusOK, responses.NewNetworkSummary(summary)) } @@ -77,7 +77,7 @@ func (sh StatsHandler) Series(c echo.Context) error { storage.NewSeriesRequest(req.From, req.To), ) if err != nil { - return internalServerError(c, err) + return handleError(c, err, sh.rollups) } response := make([]responses.SeriesItem, len(histogram)) @@ -135,7 +135,7 @@ func (sh StatsHandler) RollupSeries(c echo.Context) error { storage.NewSeriesRequest(req.From, req.To), ) if err != nil { - return internalServerError(c, err) + return handleError(c, err, sh.rollups) } response := make([]responses.RollupSeriesItem, len(histogram)) diff --git a/cmd/api/init.go b/cmd/api/init.go index 42974c0..3073312 100644 --- a/cmd/api/init.go +++ b/cmd/api/init.go @@ -139,6 +139,16 @@ func cacheSkipper(c echo.Context) bool { func initEcho(cfg ApiConfig, db postgres.Storage, env string) *echo.Echo { e := echo.New() e.Validator = handler.NewApiValidator() + + timeout := 30 * time.Second + if cfg.RequestTimeout > 0 { + timeout = time.Duration(cfg.RequestTimeout) * time.Second + } + e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{ + Skipper: websocketSkipper, + Timeout: timeout, + })) + e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{ LogURI: true, LogStatus: true, @@ -197,15 +207,6 @@ func initEcho(cfg ApiConfig, db postgres.Storage, env string) *echo.Echo { e.Use(middleware.Secure()) e.Pre(middleware.RemoveTrailingSlash()) - timeout := 30 * time.Second - if cfg.RequestTimeout > 0 { - timeout = time.Duration(cfg.RequestTimeout) * time.Second - } - e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{ - Skipper: websocketSkipper, - Timeout: timeout, - })) - if cfg.Prometheus { e.Use(echoprometheus.NewMiddlewareWithConfig(echoprometheus.MiddlewareConfig{ Namespace: "astria_api",