Skip to content

Commit e5ed4d6

Browse files
committed
Add error handling to blockfrost submit_tx method
1 parent 9485ce5 commit e5ed4d6

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

pycardano/backend/blockfrost.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Dict, List, Optional, Union
66

77
import cbor2
8-
from blockfrost import ApiUrls, BlockFrostApi
8+
from blockfrost import ApiError, ApiUrls, BlockFrostApi
99
from blockfrost.utils import Namespace
1010

1111
from pycardano.address import Address
@@ -222,13 +222,31 @@ def utxos(self, address: str) -> List[UTxO]:
222222

223223
return utxos
224224

225-
def submit_tx(self, cbor: Union[bytes, str]):
225+
def submit_tx(self, cbor: Union[bytes, str]) -> str:
226+
"""Submit a transaction.
227+
228+
Args:
229+
cbor (Union[bytes, str]): The serialized transaction to be submitted.
230+
231+
Returns:
232+
str: The transaction hash.
233+
234+
Raises:
235+
:class:`TransactionFailedException`: When fails to submit the transaction.
236+
"""
226237
if isinstance(cbor, str):
227238
cbor = bytes.fromhex(cbor)
228239
with tempfile.NamedTemporaryFile(delete=False) as f:
229240
f.write(cbor)
230-
self.api.transaction_submit(f.name)
241+
try:
242+
response = self.api.transaction_submit(f.name)
243+
except ApiError as e:
244+
os.remove(f.name)
245+
raise TransactionFailedException(
246+
f"Failed to submit transaction. Error code: {e.status_code}. Error message: {e.message}"
247+
) from e
231248
os.remove(f.name)
249+
return response
232250

233251
def evaluate_tx(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]:
234252
"""Evaluate execution units of a transaction.

0 commit comments

Comments
 (0)