Skip to content

Commit

Permalink
Improve transaction status error handling
Browse files Browse the repository at this point in the history
Introduce a dedicated error variable for transaction not found cases in the
adapter layer. This separates internal error handling from API errors and
follows Go best practices by using errors.Is() for comparisons.

The change:
- Adds errTransactionNotFound for internal error handling
- Replaces string-based error comparison with errors.Is()
- Maintains existing API behavior while improving code maintainability
  • Loading branch information
wojciechos committed Dec 5, 2024
1 parent c248fd2 commit fdcbe48
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ func (h *Handler) AddTransaction(ctx context.Context, tx BroadcastedTransaction)
}, nil
}

var errTransactionNotFound = fmt.Errorf("transaction not found")

func (h *Handler) TransactionStatus(ctx context.Context, hash felt.Felt) (*TransactionStatus, *jsonrpc.Error) {
receipt, txErr := h.TransactionReceiptByHash(hash)
switch txErr {
Expand All @@ -585,7 +587,7 @@ func (h *Handler) TransactionStatus(ctx context.Context, hash felt.Felt) (*Trans

status, err := adaptTransactionStatus(txStatus)
if err != nil {
if !errors.Is(err, fmt.Errorf("%s", ErrTxnHashNotFound.Message)) {
if !errors.Is(err, errTransactionNotFound) {
h.log.Errorw("Failed to adapt transaction status", "err", err)
}

Check warning on line 592 in rpc/transaction.go

View check run for this annotation

Codecov / codecov/patch

rpc/transaction.go#L591-L592

Added lines #L591 - L592 were not covered by tests
return nil, ErrTxnHashNotFound
Expand Down Expand Up @@ -753,7 +755,7 @@ func adaptTransactionStatus(txStatus *starknet.TransactionStatus) (*TransactionS
case starknet.Received:
status.Finality = TxnStatusReceived
case starknet.NotReceived:
return nil, fmt.Errorf("%s", ErrTxnHashNotFound.Message)
return nil, errTransactionNotFound
default:
return nil, fmt.Errorf("unknown finality status: %v", finalityStatus)
}
Expand Down

0 comments on commit fdcbe48

Please sign in to comment.