|
| 1 | +# ReceiptStatusError |
| 2 | + |
| 3 | +This guide explains `ReceiptStatusError` in the Hiero SDK, how it differs from other errors, and how to handle it effectively. |
| 4 | + |
| 5 | +## Error Handling Overview |
| 6 | + |
| 7 | +Many developers assume that if `execute()` doesn't throw an exception, the transaction or query succeeded. However, failures can occur at different stages: |
| 8 | + |
| 9 | +1. **Precheck (Before Submission):** |
| 10 | + * Occurs if the transaction is malformed or fails initial validation by the node. |
| 11 | + * The SDK raises a `PrecheckError` (or similar) immediately. |
| 12 | + * The transaction never reaches consensus. |
| 13 | + |
| 14 | +2. **Network/Node Retry Failures:** |
| 15 | + * Occurs if the SDK cannot reach a node or receives transient errors (e.g., `BUSY`). |
| 16 | + * The SDK automatically retries up to a limit. |
| 17 | + * If retries are exhausted, a `MaxAttemptsError` or `TimeoutError` is raised. |
| 18 | + |
| 19 | +3. **Receipt Status Errors (Post-Consensus):** |
| 20 | + * Occurs **after** the network reaches consensus on the transaction. |
| 21 | + * The transaction was successfully submitted and processed, but the logic failed (e.g., insufficient funds, token supply full, invalid signature for the specific operation). |
| 22 | + * This is where `ReceiptStatusError` comes in. |
| 23 | + |
| 24 | +## What is ReceiptStatusError? |
| 25 | + |
| 26 | +`ReceiptStatusError` is an exception that represents a failure during the **consensus/execution** phase. |
| 27 | + |
| 28 | +* **Timing:** It happens after the transaction is submitted and processed by the network. |
| 29 | +* **Content:** It contains the `transaction_receipt`, which holds the status code (e.g., `INSUFFICIENT_ACCOUNT_BALANCE`) and other metadata. |
| 30 | +* **Usage:** You must explicitly check the receipt status or configure your client/transaction to throw this error. |
| 31 | + |
| 32 | +## Handling ReceiptStatusError |
| 33 | + |
| 34 | +When you execute a transaction, you typically get a receipt. You should check the status of this receipt. |
| 35 | + |
| 36 | +```python |
| 37 | +from hiero_sdk_python.response_code import ResponseCode |
| 38 | +from hiero_sdk_python.exceptions import ReceiptStatusError |
| 39 | + |
| 40 | +# ... client and transaction setup ... |
| 41 | + |
| 42 | +try: |
| 43 | + # Execute the transaction |
| 44 | + receipt = tx.execute(client) |
| 45 | + |
| 46 | + # Check if the status is SUCCESS |
| 47 | + if receipt.status != ResponseCode.SUCCESS: |
| 48 | + # Raise the specific error with details |
| 49 | + raise ReceiptStatusError(receipt.status, tx.transaction_id, receipt) |
| 50 | + |
| 51 | + print("Transaction succeeded!") |
| 52 | + |
| 53 | +except ReceiptStatusError as e: |
| 54 | + print(f"Transaction failed post-consensus: {e}") |
| 55 | + print(f"Status Code: {e.status}") |
| 56 | + print(f"Transaction ID: {e.transaction_id}") |
| 57 | + # You can access the full receipt for debugging |
| 58 | + # print(e.transaction_receipt) |
| 59 | + |
| 60 | +except Exception as e: |
| 61 | + print(f"An unexpected error occurred: {e}") |
| 62 | +``` |
| 63 | + |
| 64 | +## Summary |
| 65 | + |
| 66 | +* **Precheck errors** happen *before* the network processes the transaction. |
| 67 | +* **ReceiptStatusErrors** happen *after* the network processes the transaction but finds it invalid according to ledger rules. |
| 68 | +* Always check `receipt.status` to ensure your transaction actually did what you intended. |
0 commit comments