Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: cancellation request error #16

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/api/handler/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package handler

import (
"context"
"net/http"

sentryecho "github.com/getsentry/sentry-go/echo"
Expand All @@ -13,6 +14,7 @@ import (

var (
errInvalidAddress = errors.New("invalid address")
errCancelRequest = "pq: canceling statement due to user request"
)

type NoRows interface {
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/api/handler/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handler/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions cmd/api/handler/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
19 changes: 10 additions & 9 deletions cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
Loading