From cb3b98b8c69896e108780450c30ae22fcb016854 Mon Sep 17 00:00:00 2001 From: Philipp Gackstatter Date: Tue, 12 Mar 2024 16:32:53 +0800 Subject: [PATCH] Fix wrap order and improve errors in components --- components/dashboard/explorer_routes.go | 18 +++++++++--------- components/inx/server_blocks.go | 2 +- components/inx/server_commitments.go | 6 +++--- components/inx/server_issuance.go | 7 +++++-- components/inx/server_transactions.go | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/components/dashboard/explorer_routes.go b/components/dashboard/explorer_routes.go index 1af8a1dfe..6766c26d6 100644 --- a/components/dashboard/explorer_routes.go +++ b/components/dashboard/explorer_routes.go @@ -28,12 +28,12 @@ func setupExplorerRoutes(routeGroup *echo.Group) { routeGroup.GET("/block/:"+api.ParameterBlockID, func(c echo.Context) (err error) { blockID, err := httpserver.ParseBlockIDParam(c, api.ParameterBlockID) if err != nil { - return ierrors.Errorf("parse block ID error: %w", err) + return ierrors.Wrap(err, "failed to parse block ID") } t, err := findBlock(blockID) if err != nil { - return ierrors.Errorf("find block error: %w", err) + return err } return c.JSON(http.StatusOK, t) @@ -53,12 +53,12 @@ func setupExplorerRoutes(routeGroup *echo.Group) { blockID, err := iotago.BlockIDFromHexString(search) if err != nil { - return ierrors.Wrapf(ErrInvalidParameter, "search ID %s", search) + return ierrors.WithMessagef(ErrInvalidParameter, "search ID %s", search) } blk, err := findBlock(blockID) if err != nil { - return ierrors.Errorf("can't find block %s: %w", search, err) + return err } result.Block = blk @@ -75,14 +75,14 @@ func setupExplorerRoutes(routeGroup *echo.Group) { func findBlock(blockID iotago.BlockID) (explorerBlk *ExplorerBlock, err error) { block, exists := deps.Protocol.Engines.Main.Get().Block(blockID) if !exists { - return nil, ierrors.Errorf("block not found: %s", blockID.ToHex()) + return nil, ierrors.Errorf("block %s not found", blockID.ToHex()) } cachedBlock, _ := deps.Protocol.Engines.Main.Get().BlockCache.Block(blockID) blockMetadata, err := deps.Protocol.Engines.Main.Get().BlockRetainer.BlockMetadata(blockID) if err != nil { - return nil, ierrors.Wrapf(err, "block metadata %s", blockID.ToHex()) + return nil, ierrors.Wrapf(err, "failed to get block metadata for block %s", blockID.ToHex()) } return createExplorerBlock(block, cachedBlock, blockMetadata), nil @@ -201,12 +201,12 @@ func getTransaction(c echo.Context) error { block, exists := deps.Protocol.Engines.Main.Get().Block(output.BlockID()) if !exists { - return ierrors.Errorf("block not found: %s", output.BlockID().ToHex()) + return ierrors.Errorf("block %s not found", output.BlockID().ToHex()) } iotaTX, isTX := block.SignedTransaction() if !isTX { - return ierrors.Errorf("payload is not a signed transaction: %s", output.BlockID().ToHex()) + return ierrors.Errorf("block payload of block %s is not a signed transaction", output.BlockID().ToHex()) } return httpserver.JSONResponse(c, http.StatusOK, NewTransaction(iotaTX)) @@ -223,7 +223,7 @@ func getTransactionMetadata(c echo.Context) error { copy(outputID[:], txID[:]) txMetadata, exists := deps.Protocol.Engines.Main.Get().Ledger.MemPool().TransactionMetadata(txID) if !exists { - return ierrors.Errorf("tx metadata not found: %s", txID.ToHex()) + return ierrors.Errorf("transaction metadata for transaction %s not found", txID.ToHex()) } conflicts, _ := deps.Protocol.Engines.Main.Get().Ledger.SpendDAG().ConflictingSpenders(txID) diff --git a/components/inx/server_blocks.go b/components/inx/server_blocks.go index 7ed7ca157..71f79981a 100644 --- a/components/inx/server_blocks.go +++ b/components/inx/server_blocks.go @@ -197,7 +197,7 @@ func (s *Server) attachBlock(ctx context.Context, block *iotago.Block) (*inx.Blo func getINXBlockMetadata(blockID iotago.BlockID) (*inx.BlockMetadata, error) { blockMetadata, err := deps.Protocol.Engines.Main.Get().BlockRetainer.BlockMetadata(blockID) if err != nil { - return nil, ierrors.Errorf("failed to get BlockMetadata: %v", err) + return nil, ierrors.Wrap(err, "failed to get BlockMetadata") } return inx.WrapBlockMetadata(blockMetadata) diff --git a/components/inx/server_commitments.go b/components/inx/server_commitments.go index c69bf74a8..3f504e15d 100644 --- a/components/inx/server_commitments.go +++ b/components/inx/server_commitments.go @@ -40,7 +40,7 @@ func (s *Server) ListenToCommitments(req *inx.SlotRangeRequest, srv inx.INX_List } if err := srv.Send(inxCommitment(commitment)); err != nil { - return ierrors.Errorf("send error: %w", err) + return ierrors.Wrap(err, "send error") } return nil @@ -108,7 +108,7 @@ func (s *Server) ListenToCommitments(req *inx.SlotRangeRequest, srv inx.INX_List catchUpFunc := func(start iotago.SlotIndex, end iotago.SlotIndex) error { err := sendSlotsRange(start, end) if err != nil { - err := ierrors.Errorf("sendSlotsRange error: %w", err) + err := ierrors.Wrap(err, "sendSlotsRange error") Component.LogError(err.Error()) return err @@ -119,7 +119,7 @@ func (s *Server) ListenToCommitments(req *inx.SlotRangeRequest, srv inx.INX_List sendFunc := func(_ iotago.SlotIndex, payload *inx.Commitment) error { if err := srv.Send(payload); err != nil { - err := ierrors.Errorf("send error: %w", err) + err := ierrors.Wrap(err, "send error") Component.LogError(err.Error()) return err diff --git a/components/inx/server_issuance.go b/components/inx/server_issuance.go index 50d43f2ae..4f1433db9 100644 --- a/components/inx/server_issuance.go +++ b/components/inx/server_issuance.go @@ -3,7 +3,6 @@ package inx import ( "context" - "github.com/iotaledger/hive.go/ierrors" "github.com/iotaledger/hive.go/lo" "github.com/iotaledger/hive.go/serializer/v2/serix" inx "github.com/iotaledger/inx/go" @@ -58,8 +57,12 @@ func (s *Server) ValidatePayload(_ context.Context, payload *inx.RawPayload) (*i // TaggedData is always valid if serix decoding was successful return nil + case *iotago.CandidacyAnnouncement: + panic("TODO: implement me") default: - return ierrors.Errorf("unsupported payload type: %T", typedPayload) + // We're switching on the Go payload type here, so we can only run into the default case + // if we added a new payload type and have not handled it above. In this case we want to panic. + panic("all supported payload types should be handled above") } }(); err != nil { //nolint:nilerr // this is expected behavior diff --git a/components/inx/server_transactions.go b/components/inx/server_transactions.go index bcf678a29..3e2063523 100644 --- a/components/inx/server_transactions.go +++ b/components/inx/server_transactions.go @@ -20,7 +20,7 @@ func (s *Server) ReadTransactionMetadata(_ context.Context, transactionID *inx.T return nil, status.Errorf(codes.NotFound, "transaction metadata not found: %s", txID.ToHex()) } - return nil, ierrors.WithMessagef(err, "error when retrieving transaction metadata: %s", txID.ToHex()) + return nil, ierrors.Wrapf(err, "error when retrieving transaction metadata: %s", txID.ToHex()) } return inx.WrapTransactionMetadata(txMetadata), nil