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: nil pointer with no content #18

Merged
merged 1 commit into from
Nov 1, 2023
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
28 changes: 14 additions & 14 deletions cmd/api/handler/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (handler *AddressHandler) Get(c echo.Context) error {
}

address, err := handler.address.ByHash(c.Request().Context(), hash)
if err := handleError(c, err, handler.address); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}

return c.JSON(http.StatusOK, responses.NewAddress(address))
Expand Down Expand Up @@ -99,8 +99,8 @@ func (handler *AddressHandler) List(c echo.Context) error {
}

address, err := handler.address.ListWithBalance(c.Request().Context(), fltrs)
if err := handleError(c, err, handler.address); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}

response := make([]responses.Address, len(address))
Expand Down Expand Up @@ -144,8 +144,8 @@ func (handler *AddressHandler) Transactions(c echo.Context) error {
}

address, err := handler.address.ByHash(c.Request().Context(), hash)
if err := handleError(c, err, handler.address); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}

fltrs := storage.TxFilter{
Expand All @@ -163,8 +163,8 @@ func (handler *AddressHandler) Transactions(c echo.Context) error {
}

txs, err := handler.txs.ByAddress(c.Request().Context(), address.Id, fltrs)
if err := handleError(c, err, handler.txs); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}
response := make([]responses.Tx, len(txs))
for i := range txs {
Expand Down Expand Up @@ -226,14 +226,14 @@ func (handler *AddressHandler) Messages(c echo.Context) error {
}

address, err := handler.address.ByHash(c.Request().Context(), hash)
if err := handleError(c, err, handler.address); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}

filters := req.ToFilters()
msgs, err := handler.address.Messages(c.Request().Context(), address.Id, filters)
if err := handleError(c, err, handler.txs); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}

response := make([]responses.Message, len(msgs))
Expand All @@ -256,8 +256,8 @@ func (handler *AddressHandler) Messages(c echo.Context) error {
// @Router /v1/address/count [get]
func (handler *AddressHandler) Count(c echo.Context) error {
state, err := handler.state.ByName(c.Request().Context(), handler.indexerName)
if err := handleError(c, err, handler.state); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}
return c.JSON(http.StatusOK, state.TotalAccounts)
}
35 changes: 18 additions & 17 deletions cmd/api/handler/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package handler

import (
"github.com/celenium-io/celestia-indexer/pkg/types"
"net/http"

"github.com/celenium-io/celestia-indexer/pkg/types"

"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -76,8 +77,8 @@ func (handler *BlockHandler) Get(c echo.Context) error {
block, err = handler.block.ByHeight(c.Request().Context(), req.Height)
}

if err := handleError(c, err, handler.block); err != nil {
return err
if err != nil {
return handleError(c, err, handler.block)
}

return c.JSON(http.StatusOK, responses.NewBlock(block, req.Stats))
Expand Down Expand Up @@ -128,8 +129,8 @@ func (handler *BlockHandler) List(c echo.Context) error {
blocks, err = handler.block.List(c.Request().Context(), req.Limit, req.Offset, pgSort(req.Sort))
}

if err := handleError(c, err, handler.block); err != nil {
return err
if err != nil {
return handleError(c, err, handler.block)
}

response := make([]responses.Block, len(blocks))
Expand Down Expand Up @@ -159,8 +160,8 @@ func (handler *BlockHandler) GetEvents(c echo.Context) error {
}

events, err := handler.events.ByBlock(c.Request().Context(), req.Height)
if err := handleError(c, err, handler.events); err != nil {
return err
if err != nil {
return handleError(c, err, handler.block)
}

response := make([]responses.Event, len(events))
Expand Down Expand Up @@ -190,8 +191,8 @@ func (handler *BlockHandler) GetStats(c echo.Context) error {
}

stats, err := handler.blockStats.ByHeight(c.Request().Context(), req.Height)
if err := handleError(c, err, handler.events); err != nil {
return err
if err != nil {
return handleError(c, err, handler.block)
}
return c.JSON(http.StatusOK, responses.NewBlockStats(stats))
}
Expand All @@ -218,14 +219,14 @@ func (handler *BlockHandler) GetNamespaces(c echo.Context) error {
req.SetDefault()

messages, err := handler.namespace.MessagesByHeight(c.Request().Context(), req.Height, int(req.Limit), int(req.Offset))
if err := handleError(c, err, handler.events); err != nil {
return err
if err != nil {
return handleError(c, err, handler.block)
}
response := make([]responses.NamespaceMessage, len(messages))
for i := range response {
msg, err := responses.NewNamespaceMessage(messages[i])
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.block)
}
response[i] = msg
}
Expand All @@ -251,8 +252,8 @@ func (handler *BlockHandler) GetNamespacesCount(c echo.Context) error {
}

count, err := handler.namespace.CountMessagesByHeight(c.Request().Context(), req.Height)
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.block)
}

return c.JSON(http.StatusOK, count)
Expand All @@ -270,8 +271,8 @@ func (handler *BlockHandler) GetNamespacesCount(c echo.Context) error {
// @Router /v1/block/count [get]
func (handler *BlockHandler) Count(c echo.Context) error {
state, err := handler.state.ByName(c.Request().Context(), handler.indexerName)
if err := handleError(c, err, handler.state); err != nil {
return err
if err != nil {
return handleError(c, err, handler.block)
}
return c.JSON(http.StatusOK, state.LastHeight+1) // + genesis block
}
24 changes: 23 additions & 1 deletion cmd/api/handler/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ package handler

import (
"context"
"database/sql"
"encoding/json"
pkgTypes "github.com/celenium-io/celestia-indexer/pkg/types"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

pkgTypes "github.com/celenium-io/celestia-indexer/pkg/types"

"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/internal/storage/mock"
Expand Down Expand Up @@ -117,6 +119,26 @@ func (s *BlockTestSuite) TestGet() {
s.Require().Nil(block.Stats)
}

func (s *BlockTestSuite) TestGetNoContent() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/block/:height")
c.SetParamNames("height")
c.SetParamValues("100")

s.blocks.EXPECT().
ByHeight(gomock.Any(), pkgTypes.Level(100)).
Return(storage.Block{}, sql.ErrNoRows)

s.blocks.EXPECT().
IsNoRows(gomock.Any()).
Return(true)

s.Require().NoError(s.handler.Get(c))
s.Require().Equal(http.StatusNoContent, rec.Code)
}

func (s *BlockTestSuite) TestGetWithoutStats() {
q := make(url.Values)
q.Set("stats", "false")
Expand Down
8 changes: 4 additions & 4 deletions cmd/api/handler/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func NewConstantHandler(constants storage.IConstant, denomMetadata storage.IDeno
// @Router /v1/constants [get]
func (handler *ConstantHandler) Get(c echo.Context) error {
consts, err := handler.constants.All(c.Request().Context())
if err := handleError(c, err, handler.address); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}
dm, err := handler.denomMetadata.All(c.Request().Context())
if err := handleError(c, err, handler.address); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}
return c.JSON(http.StatusOK, responses.NewConstants(consts, dm))
}
39 changes: 20 additions & 19 deletions cmd/api/handler/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package handler
import (
"encoding/base64"
"encoding/hex"
"github.com/celenium-io/celestia-indexer/pkg/types"
"net/http"

"github.com/celenium-io/celestia-indexer/pkg/types"

"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/pkg/node"
Expand Down Expand Up @@ -65,8 +66,8 @@ func (handler *NamespaceHandler) Get(c echo.Context) error {
}

namespace, err := handler.namespace.ByNamespaceId(c.Request().Context(), namespaceId)
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}

response := make([]responses.Namespace, len(namespace))
Expand Down Expand Up @@ -111,8 +112,8 @@ func (handler *NamespaceHandler) GetByHash(c echo.Context) error {
namespaceId := hash[1:]

namespace, err := handler.namespace.ByNamespaceIdAndVersion(c.Request().Context(), namespaceId, version)
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}
return c.JSON(http.StatusOK, responses.NewNamespace(namespace))
}
Expand Down Expand Up @@ -148,8 +149,8 @@ func (handler *NamespaceHandler) GetWithVersion(c echo.Context) error {
}

namespace, err := handler.namespace.ByNamespaceIdAndVersion(c.Request().Context(), namespaceId, req.Version)
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}

return c.JSON(http.StatusOK, responses.NewNamespace(namespace))
Expand Down Expand Up @@ -177,8 +178,8 @@ func (handler *NamespaceHandler) List(c echo.Context) error {
req.SetDefault()

namespace, err := handler.namespace.List(c.Request().Context(), req.Limit, req.Offset, pgSort(req.Sort))
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}
response := make([]responses.Namespace, len(namespace))
for i := range namespace {
Expand Down Expand Up @@ -286,20 +287,20 @@ func (handler *NamespaceHandler) GetMessages(c echo.Context) error {
}

ns, err := handler.namespace.ByNamespaceIdAndVersion(c.Request().Context(), namespaceId, req.Version)
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}

messages, err := handler.namespace.Messages(c.Request().Context(), ns.Id, int(req.Limit), int(req.Offset))
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}

response := make([]responses.NamespaceMessage, len(messages))
for i := range response {
msg, err := responses.NewNamespaceMessage(messages[i])
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}
response[i] = msg
}
Expand All @@ -319,8 +320,8 @@ func (handler *NamespaceHandler) GetMessages(c echo.Context) error {
// @Router /v1/namespace/active [get]
func (handler *NamespaceHandler) GetActive(c echo.Context) error {
active, err := handler.namespace.Active(c.Request().Context(), 5)
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}

response := make([]responses.ActiveNamespace, len(active))
Expand All @@ -342,8 +343,8 @@ func (handler *NamespaceHandler) GetActive(c echo.Context) error {
// @Router /v1/namespace/count [get]
func (handler *NamespaceHandler) Count(c echo.Context) error {
state, err := handler.state.ByName(c.Request().Context(), handler.indexerName)
if err := handleError(c, err, handler.state); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}
return c.JSON(http.StatusOK, state.TotalNamespaces)
}
8 changes: 4 additions & 4 deletions cmd/api/handler/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func (handler SearchHandler) searchAddress(c echo.Context, search string) error
}

address, err := handler.address.ByHash(c.Request().Context(), hash)
if err := handleError(c, err, handler.address); err != nil {
return err
if err != nil {
return handleError(c, err, handler.address)
}
return c.JSON(http.StatusOK, responses.NewSearchResponse(responses.NewAddress(address)))
}
Expand Down Expand Up @@ -157,8 +157,8 @@ func (handler SearchHandler) getNamespace(c echo.Context, data []byte) error {
version := data[0]
namespaceId := data[1:]
ns, err := handler.namespace.ByNamespaceIdAndVersion(c.Request().Context(), namespaceId, version)
if err := handleError(c, err, handler.namespace); err != nil {
return err
if err != nil {
return handleError(c, err, handler.namespace)
}
response := responses.NewNamespace(ns)
return c.JSON(http.StatusOK, responses.NewSearchResponse(response))
Expand Down
Loading
Loading