Skip to content

Commit

Permalink
Merge pull request #366 from onflow/gregor/error-handling
Browse files Browse the repository at this point in the history
Improve error handling
  • Loading branch information
sideninja authored Jul 19, 2024
2 parents e648106 + 0a407df commit d37bd82
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
24 changes: 11 additions & 13 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,7 @@ func (b *BlockChainAPI) SendRawTransaction(

id, err := b.evm.SendRawTransaction(ctx, input)
if err != nil {
var errGasPriceTooLow *errs.GasPriceTooLowError

// handle typed errors
switch {
case errors.As(err, &errGasPriceTooLow):
return common.Hash{}, errGasPriceTooLow
case errors.Is(err, models.ErrInvalidEVMTransaction):
return common.Hash{}, err
default:
return common.Hash{}, errs.ErrInternal
}
return handleError[common.Hash](b.logger, err)
}

return id, nil
Expand Down Expand Up @@ -912,17 +902,25 @@ func (b *BlockChainAPI) GetStorageAt(
// if the error is not of type ErrNotFound it will return the error and the generic
// empty type.
func handleError[T any](log zerolog.Logger, err error) (T, error) {
var zero T
var (
zero T
errGasPriceTooLow *errs.GasPriceTooLowError
)

switch {
// as per specification returning nil and nil for not found resources
case errors.Is(err, storageErrs.ErrNotFound):
return zero, nil
case errors.Is(err, storageErrs.ErrInvalidRange):
return zero, err
case errors.Is(err, models.ErrInvalidEVMTransaction):
return zero, err
case errors.Is(err, requester.ErrOutOfRange):
return zero, fmt.Errorf("requested height is out of supported range")
return zero, err
case errors.Is(err, errs.ErrInvalid):
return zero, err
case errors.As(err, &errGasPriceTooLow):
return zero, errGasPriceTooLow
default:
log.Error().Err(err).Msg("api error")
return zero, errs.ErrInternal
Expand Down
2 changes: 1 addition & 1 deletion models/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
func decodeBlock(event cadence.Event) (*types.Block, error) {
payload, err := types.DecodeBlockEventPayload(event)
if err != nil {
return nil, fmt.Errorf("failed to cadence decode block: %w", err)
return nil, fmt.Errorf("failed to cadence decode block [%s]: %w", event.String(), err)
}

hashes := make([]common.Hash, len(payload.TransactionHashes))
Expand Down
18 changes: 9 additions & 9 deletions models/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,24 @@ func decodeTransactionEvent(
) (Transaction, *StorageReceipt, error) {
txEvent, err := types.DecodeTransactionEventPayload(event)
if err != nil {
return nil, nil, fmt.Errorf("failed to Cadence decode transaction event: %w", err)
return nil, nil, fmt.Errorf("failed to Cadence decode transaction event [%s]: %w", event.String(), err)
}

encodedTx, err := hex.DecodeString(txEvent.Payload)
if err != nil {
return nil, nil, fmt.Errorf("failed to hex-decode transaction payload: %w", err)
return nil, nil, fmt.Errorf("failed to hex decode transaction payload [%s]: %w", txEvent.Payload, err)
}

encodedLogs, err := hex.DecodeString(txEvent.Logs)
if err != nil {
return nil, nil, fmt.Errorf("failed to hex decode receipt: %w", err)
return nil, nil, fmt.Errorf("failed to hex decode transaction logs [%s]: %w", txEvent.Logs, err)
}

var logs []*gethTypes.Log
if len(encodedLogs) > 0 {
err = rlp.Decode(bytes.NewReader(encodedLogs), &logs)
if err != nil {
return nil, nil, fmt.Errorf("failed to RLP-decode receipt: %w", err)
return nil, nil, fmt.Errorf("failed to RLP-decode transaction logs [%x]: %w", encodedLogs, err)
}
}

Expand Down Expand Up @@ -235,7 +235,7 @@ func decodeTransactionEvent(
if txEvent.ErrorCode == uint16(types.ExecutionErrCodeExecutionReverted) {
revert, err := hex.DecodeString(txEvent.ReturnedData)
if err != nil {
return nil, nil, fmt.Errorf("failed to hex-decode transaction return data: %w", err)
return nil, nil, fmt.Errorf("failed to hex-decode transaction return data [%s]: %w", txEvent.ReturnedData, err)
}
receipt.RevertReason = revert
}
Expand All @@ -246,15 +246,15 @@ func decodeTransactionEvent(
if txEvent.TransactionType == types.DirectCallTxType {
directCall, err := types.DirectCallFromEncoded(encodedTx)
if err != nil {
return nil, nil, fmt.Errorf("failed to RLP-decode direct call: %w", err)
return nil, nil, fmt.Errorf("failed to RLP-decode direct call [%x]: %w", encodedTx, err)
}
evmHeight := receipt.BlockNumber.Uint64()

tx = DirectCall{DirectCall: directCall, blockHeight: evmHeight}
} else {
gethTx := &gethTypes.Transaction{}
if err := gethTx.UnmarshalBinary(encodedTx); err != nil {
return nil, nil, fmt.Errorf("failed to RLP-decode transaction: %w", err)
return nil, nil, fmt.Errorf("failed to RLP-decode transaction [%x]: %w", encodedTx, err)
}
tx = TransactionCall{Transaction: gethTx}
}
Expand All @@ -269,7 +269,7 @@ func UnmarshalTransaction(value []byte, blockHeight uint64) (Transaction, error)
if value[0] == types.DirectCallTxType {
directCall, err := types.DirectCallFromEncoded(value)
if err != nil {
return nil, fmt.Errorf("failed to RLP-decode direct call: %w", err)
return nil, fmt.Errorf("failed to RLP-decode direct call [%x]: %w", value, err)
}

// TEMP: Remove `blockHeight` after PreviewNet is reset
Expand All @@ -284,7 +284,7 @@ func UnmarshalTransaction(value []byte, blockHeight uint64) (Transaction, error)
return TransactionCall{Transaction: tx}, nil
}

return nil, fmt.Errorf("failed to RLP-decode transaction: %w", err)
return nil, fmt.Errorf("failed to RLP-decode transaction [%x]: %w", value, err)
}

return TransactionCall{Transaction: tx}, nil
Expand Down
2 changes: 1 addition & 1 deletion services/requester/cross-spork_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"golang.org/x/exp/slices"
)

var ErrOutOfRange = errors.New("height is out of range for provided spork clients")
var ErrOutOfRange = errors.New("height is out of available range")

type sporkClient struct {
firstHeight uint64
Expand Down
15 changes: 12 additions & 3 deletions services/requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func (e *EVM) Call(

evmResult, err := stdlib.ResultSummaryFromEVMResultValue(scriptResult)
if err != nil {
return nil, fmt.Errorf("failed to decode EVM result from call: %w", err)
return nil, fmt.Errorf("failed to decode EVM result from call [%s]: %w", scriptResult.String(), err)
}

if evmResult.ErrorCode != 0 {
Expand Down Expand Up @@ -677,12 +677,21 @@ func (e *EVM) executeScriptAtHeight(
)
}

return e.client.ExecuteScriptAtBlockHeight(
res, err := e.client.ExecuteScriptAtBlockHeight(
ctx,
height,
e.replaceAddresses(script),
arguments,
)
if err != nil {
// if snapshot doesn't exist on EN, the height at which script was executed is out
// of the boundaries the EN keeps state, so return out of range
if strings.Contains(err.Error(), "failed to create storage snapshot") {
return nil, ErrOutOfRange
}
}

return res, err
}

func addressToCadenceString(address common.Address) (cadence.String, error) {
Expand All @@ -699,7 +708,7 @@ func cadenceStringToBytes(value cadence.Value) ([]byte, error) {

code, err := hex.DecodeString(string(cdcString))
if err != nil {
return nil, fmt.Errorf("failed to decode string to byte array: %w", err)
return nil, fmt.Errorf("failed to decode string to byte array [%s]: %w", cdcString, err)
}

return code, nil
Expand Down

0 comments on commit d37bd82

Please sign in to comment.