Skip to content

Commit

Permalink
Merge pull request #463 from iotaledger/api-error-codes
Browse files Browse the repository at this point in the history
Set the error codes in api responses
  • Loading branch information
alexsporn authored Oct 27, 2023
2 parents 7afa37c + b861e05 commit c6155f9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
26 changes: 13 additions & 13 deletions components/restapi/core/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func congestionForAccountID(c echo.Context) (*apimodels.CongestionResponse, erro

acc, exists, err := deps.Protocol.MainEngineInstance().Ledger.Account(accountID, commitment.Slot())
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get account: %s form the Ledger", accountID.ToHex())
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get account %s from the Ledger: %s", accountID.ToHex(), err)
}
if !exists {
return nil, ierrors.Errorf("account not found: %s", accountID.ToHex())
return nil, ierrors.Wrapf(echo.ErrNotFound, "account not found: %s", accountID.ToHex())
}

return &apimodels.CongestionResponse{
Expand All @@ -46,7 +46,7 @@ func validators(c echo.Context) (*apimodels.ValidatorsResponse, error) {
if len(c.QueryParam(restapipkg.QueryParameterPageSize)) > 0 {
pageSize, err = httpserver.ParseUint32QueryParam(c, restapipkg.QueryParameterPageSize)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse the %s parameter", restapipkg.QueryParameterPageSize)
return nil, ierrors.Wrapf(err, "failed to parse page size %s", c.Param(restapipkg.QueryParameterPageSize))
}
if pageSize > restapi.ParamsRestAPI.MaxPageSize {
pageSize = restapi.ParamsRestAPI.MaxPageSize
Expand All @@ -59,13 +59,13 @@ func validators(c echo.Context) (*apimodels.ValidatorsResponse, error) {
if len(c.QueryParam(restapipkg.QueryParameterCursor)) != 0 {
requestedSlot, cursorIndex, err = httpserver.ParseCursorQueryParam(c, restapipkg.QueryParameterCursor)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse the %s parameter", restapipkg.QueryParameterCursor)
return nil, ierrors.Wrapf(err, "failed to parse cursor %s", c.Param(restapipkg.QueryParameterCursor))
}
}

// do not respond to really old requests
if requestedSlot+iotago.SlotIndex(restapi.ParamsRestAPI.MaxRequestedSlotAge) < latestCommittedSlot {
return nil, ierrors.Errorf("request is too old, request started at %d, latest committed slot index is %d", requestedSlot, latestCommittedSlot)
return nil, ierrors.Wrapf(echo.ErrBadRequest, "request is too old, request started at %d, latest committed slot index is %d", requestedSlot, latestCommittedSlot)
}

nextEpoch := deps.Protocol.APIForSlot(latestCommittedSlot).TimeProvider().EpochFromSlot(latestCommittedSlot) + 1
Expand All @@ -75,7 +75,7 @@ func validators(c echo.Context) (*apimodels.ValidatorsResponse, error) {
if !exists {
registeredValidators, err = deps.Protocol.MainEngineInstance().SybilProtection.OrderedRegisteredCandidateValidatorsList(nextEpoch)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get ordered registered validators list for epoch %d", nextEpoch)
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get ordered registered validators list for epoch %d : %s", nextEpoch, err)
}
deps.Protocol.MainEngineInstance().Retainer.RetainRegisteredValidatorsCache(slotRange, registeredValidators)
}
Expand All @@ -98,16 +98,16 @@ func validators(c echo.Context) (*apimodels.ValidatorsResponse, error) {
func validatorByAccountID(c echo.Context) (*apimodels.ValidatorResponse, error) {
accountID, err := httpserver.ParseAccountIDParam(c, restapipkg.ParameterAccountID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse the %s parameter", restapipkg.ParameterAccountID)
return nil, ierrors.Wrapf(err, "failed to parse account ID %s", c.Param(restapipkg.ParameterAccountID))
}
latestCommittedSlot := deps.Protocol.MainEngineInstance().SyncManager.LatestCommitment().Slot()

accountData, exists, err := deps.Protocol.MainEngineInstance().Ledger.Account(accountID, latestCommittedSlot)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get account: %s form the Ledger", accountID.ToHex())
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get account %s from the Ledger: %s", accountID.ToHex(), err)
}
if !exists {
return nil, ierrors.Errorf("account not found: %s for latest committedSlot %d", accountID.ToHex(), latestCommittedSlot)
return nil, ierrors.Wrapf(echo.ErrNotFound, "account %s not found for latest committedSlot %d", accountID.ToHex(), latestCommittedSlot)
}
nextEpoch := deps.Protocol.APIForSlot(latestCommittedSlot).TimeProvider().EpochFromSlot(latestCommittedSlot) + 1

Expand All @@ -131,12 +131,12 @@ func validatorByAccountID(c echo.Context) (*apimodels.ValidatorResponse, error)
func rewardsByOutputID(c echo.Context) (*apimodels.ManaRewardsResponse, error) {
outputID, err := httpserver.ParseOutputIDParam(c, restapipkg.ParameterOutputID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse the %s parameter", restapipkg.ParameterOutputID)
return nil, ierrors.Wrapf(err, "failed to parse output ID %s", c.Param(restapipkg.ParameterOutputID))
}

utxoOutput, err := deps.Protocol.MainEngineInstance().Ledger.Output(outputID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get output %s from ledger", outputID.ToHex())
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get output %s from ledger: %s", outputID.ToHex(), err)
}

var reward iotago.Mana
Expand All @@ -147,7 +147,7 @@ func rewardsByOutputID(c echo.Context) (*apimodels.ManaRewardsResponse, error) {
accountOutput := utxoOutput.Output().(*iotago.AccountOutput)
feature, exists := accountOutput.FeatureSet()[iotago.FeatureStaking]
if !exists {
return nil, ierrors.Errorf("account %s is not a validator", outputID)
return nil, ierrors.Wrapf(echo.ErrBadRequest, "account %s is not a validator", outputID.ToHex())
}

//nolint:forcetypeassert
Expand Down Expand Up @@ -178,7 +178,7 @@ func rewardsByOutputID(c echo.Context) (*apimodels.ManaRewardsResponse, error) {
)
}
if err != nil {
return nil, ierrors.Wrapf(err, "failed to calculate reward for output %s", outputID)
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to calculate reward for output %s: %s", outputID.ToHex(), err)
}

return &apimodels.ManaRewardsResponse{
Expand Down
12 changes: 6 additions & 6 deletions components/restapi/core/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
func blockByID(c echo.Context) (*model.Block, error) {
blockID, err := httpserver.ParseBlockIDParam(c, restapi.ParameterBlockID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse block ID: %s", c.Param(restapi.ParameterBlockID))
return nil, ierrors.Wrapf(err, "failed to parse block ID %s", c.Param(restapi.ParameterBlockID))
}

block, exists := deps.Protocol.MainEngineInstance().Block(blockID)
if !exists {
return nil, ierrors.Errorf("block not found: %s", blockID.ToHex())
return nil, ierrors.Wrapf(echo.ErrNotFound, "block not found: %s", blockID.ToHex())
}

return block, nil
Expand All @@ -29,7 +29,7 @@ func blockByID(c echo.Context) (*model.Block, error) {
func blockMetadataByBlockID(blockID iotago.BlockID) (*apimodels.BlockMetadataResponse, error) {
blockMetadata, err := deps.Protocol.MainEngineInstance().Retainer.BlockMetadata(blockID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get block metadata: %s", blockID.ToHex())
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get block metadata %s: %s", blockID.ToHex(), err)
}

return blockMetadata.BlockMetadataResponse(), nil
Expand All @@ -38,7 +38,7 @@ func blockMetadataByBlockID(blockID iotago.BlockID) (*apimodels.BlockMetadataRes
func blockMetadataByID(c echo.Context) (*apimodels.BlockMetadataResponse, error) {
blockID, err := httpserver.ParseBlockIDParam(c, restapi.ParameterBlockID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse block ID: %s", c.Param(restapi.ParameterBlockID))
return nil, ierrors.Wrapf(err, "failed to parse block ID %s", c.Param(restapi.ParameterBlockID))
}

return blockMetadataByBlockID(blockID)
Expand All @@ -55,7 +55,7 @@ func blockIssuanceBySlot(slotIndex iotago.SlotIndex) (*apimodels.IssuanceBlockHe
} else {
slotCommitment, err = deps.Protocol.MainEngineInstance().Storage.Commitments().Load(slotIndex)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment for requested slot %d", slotIndex)
return nil, ierrors.Wrapf(echo.ErrNotFound, "failed to load commitment for requested slot %d: %s", slotIndex, err)
}
}

Expand All @@ -77,7 +77,7 @@ func blockIssuanceBySlot(slotIndex iotago.SlotIndex) (*apimodels.IssuanceBlockHe
func sendBlock(c echo.Context) (*apimodels.BlockCreatedResponse, error) {
iotaBlock, err := httpserver.ParseRequestByHeader(c, deps.Protocol.CommittedAPI(), iotago.ProtocolBlockFromBytes(deps.Protocol))
if err != nil {
return nil, err
return nil, ierrors.Wrapf(err, "failed to parse iotablock")
}

blockID, err := deps.BlockHandler.AttachBlock(c.Request().Context(), iotaBlock)
Expand Down
6 changes: 3 additions & 3 deletions components/restapi/core/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func indexByCommitmentID(c echo.Context) (iotago.SlotIndex, error) {
commitmentID, err := httpserver.ParseCommitmentIDParam(c, restapipkg.ParameterCommitmentID)
if err != nil {
return iotago.SlotIndex(0), ierrors.Wrapf(err, "failed to parse commitment ID: %s", c.Param(restapipkg.ParameterCommitmentID))
return iotago.SlotIndex(0), ierrors.Wrapf(err, "failed to parse commitment ID %s", c.Param(restapipkg.ParameterCommitmentID))
}

return commitmentID.Slot(), nil
Expand All @@ -22,7 +22,7 @@ func indexByCommitmentID(c echo.Context) (iotago.SlotIndex, error) {
func getCommitmentDetails(index iotago.SlotIndex) (*iotago.Commitment, error) {
commitment, err := deps.Protocol.MainEngineInstance().Storage.Commitments().Load(index)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment: %d", index)
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to load commitment %d: %s", index, err)
}

return commitment.Commitment(), nil
Expand All @@ -31,7 +31,7 @@ func getCommitmentDetails(index iotago.SlotIndex) (*iotago.Commitment, error) {
func getUTXOChanges(slot iotago.SlotIndex) (*apimodels.UTXOChangesResponse, error) {
diffs, err := deps.Protocol.MainEngineInstance().Ledger.SlotDiffs(slot)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get slot diffs: %d", slot)
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get slot diffs %d: %s", slot, err)
}

createdOutputs := make(iotago.OutputIDs, len(diffs.Outputs))
Expand Down
10 changes: 5 additions & 5 deletions components/restapi/core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func blockIDByTransactionID(c echo.Context) (iotago.BlockID, error) {
txID, err := httpserver.ParseTransactionIDParam(c, restapipkg.ParameterTransactionID)
if err != nil {
return iotago.EmptyBlockID, ierrors.Wrapf(err, "failed to parse transaction ID: %s", c.Param(restapipkg.ParameterTransactionID))
return iotago.EmptyBlockID, ierrors.Wrapf(err, "failed to parse transaction ID %s", c.Param(restapipkg.ParameterTransactionID))
}

return blockIDFromTransactionID(txID)
Expand All @@ -26,7 +26,7 @@ func blockIDFromTransactionID(transactionID iotago.TransactionID) (iotago.BlockI

output, spent, err := deps.Protocol.MainEngineInstance().Ledger.OutputOrSpent(outputID)
if err != nil {
return iotago.EmptyBlockID, ierrors.Wrapf(err, "failed to get output: %s", outputID.ToHex())
return iotago.EmptyBlockID, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get output %s: %s", outputID.ToHex(), err)
}

if output != nil {
Expand All @@ -39,12 +39,12 @@ func blockIDFromTransactionID(transactionID iotago.TransactionID) (iotago.BlockI
func blockByTransactionID(c echo.Context) (*model.Block, error) {
blockID, err := blockIDByTransactionID(c)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get block ID by transaction ID")
return nil, ierrors.Wrapf(echo.ErrBadRequest, "failed to get block ID by transaction ID: %s", err)
}

block, exists := deps.Protocol.MainEngineInstance().Block(blockID)
if !exists {
return nil, ierrors.Errorf("block not found: %s", blockID.String())
return nil, ierrors.Wrapf(echo.ErrNotFound, "block not found: %s", blockID.ToHex())
}

return block, nil
Expand All @@ -53,7 +53,7 @@ func blockByTransactionID(c echo.Context) (*model.Block, error) {
func blockMetadataFromTransactionID(c echo.Context) (*apimodels.BlockMetadataResponse, error) {
blockID, err := blockIDByTransactionID(c)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get block ID by transaction ID")
return nil, ierrors.Wrapf(echo.ErrBadRequest, "failed to get block ID by transaction ID: %s", err)
}

return blockMetadataByBlockID(blockID)
Expand Down
20 changes: 10 additions & 10 deletions components/restapi/core/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
func getOutput(c echo.Context) (*apimodels.OutputResponse, error) {
outputID, err := httpserver.ParseOutputIDParam(c, restapipkg.ParameterOutputID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse output ID param: %s", c.Param(restapipkg.ParameterOutputID))
return nil, ierrors.Wrapf(err, "failed to parse output ID %s", c.Param(restapipkg.ParameterOutputID))
}

output, err := deps.Protocol.MainEngineInstance().Ledger.Output(outputID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get output: %s from the Ledger", outputID.String())
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get output %s from the Ledger: %s", outputID.ToHex(), err)
}

return &apimodels.OutputResponse{
Expand All @@ -30,12 +30,12 @@ func getOutput(c echo.Context) (*apimodels.OutputResponse, error) {
func getOutputMetadata(c echo.Context) (*apimodels.OutputMetadata, error) {
outputID, err := httpserver.ParseOutputIDParam(c, restapipkg.ParameterOutputID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse output ID param: %s", c.Param(restapipkg.ParameterOutputID))
return nil, ierrors.Wrapf(err, "failed to parse output ID %s", c.Param(restapipkg.ParameterOutputID))
}

output, spent, err := deps.Protocol.MainEngineInstance().Ledger.OutputOrSpent(outputID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get output: %s from the Ledger", outputID.String())
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get output %s from the Ledger: %s", outputID.ToHex(), err)
}

if spent != nil {
Expand All @@ -48,18 +48,18 @@ func getOutputMetadata(c echo.Context) (*apimodels.OutputMetadata, error) {
func getOutputWithMetadata(c echo.Context) (*apimodels.OutputWithMetadataResponse, error) {
outputID, err := httpserver.ParseOutputIDParam(c, restapipkg.ParameterOutputID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to parse output ID param: %s", c.Param(restapipkg.ParameterOutputID))
return nil, ierrors.Wrapf(err, "failed to parse output ID %s", c.Param(restapipkg.ParameterOutputID))
}

output, spent, err := deps.Protocol.MainEngineInstance().Ledger.OutputOrSpent(outputID)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get output: %s from the Ledger", outputID.String())
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to get output %s from the Ledger: %s", outputID.ToHex(), err)
}

if spent != nil {
metadata, err := newSpentMetadataResponse(spent)
if err != nil {
return nil, err
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to load spent output metadata: %s", err)
}

return &apimodels.OutputWithMetadataResponse{
Expand Down Expand Up @@ -96,7 +96,7 @@ func newOutputMetadataResponse(output *utxoledger.Output) (*apimodels.OutputMeta
if includedSlotIndex <= latestCommitment.Slot() {
includedCommitment, err := deps.Protocol.MainEngineInstance().Storage.Commitments().Load(includedSlotIndex)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment with index: %d", includedSlotIndex)
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to load commitment with index %d: %s", includedSlotIndex, err)
}
resp.IncludedCommitmentID = includedCommitment.ID()
}
Expand All @@ -120,7 +120,7 @@ func newSpentMetadataResponse(spent *utxoledger.Spent) (*apimodels.OutputMetadat
if includedSlotIndex <= latestCommitment.Slot() {
includedCommitment, err := deps.Protocol.MainEngineInstance().Storage.Commitments().Load(includedSlotIndex)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment with index: %d", includedSlotIndex)
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to load commitment with index %d: %s", includedSlotIndex, err)
}
resp.IncludedCommitmentID = includedCommitment.ID()
}
Expand All @@ -129,7 +129,7 @@ func newSpentMetadataResponse(spent *utxoledger.Spent) (*apimodels.OutputMetadat
if spentSlotIndex <= latestCommitment.Slot() {
spentCommitment, err := deps.Protocol.MainEngineInstance().Storage.Commitments().Load(spentSlotIndex)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment with index: %d", spentSlotIndex)
return nil, ierrors.Wrapf(echo.ErrInternalServerError, "failed to load commitment with index %d: %s", spentSlotIndex, err)
}
resp.CommitmentIDSpent = spentCommitment.ID()
}
Expand Down

0 comments on commit c6155f9

Please sign in to comment.