1+ from __future__ import annotations
2+ from typing import TYPE_CHECKING , Optional , Union
3+
14from hiero_sdk_python .response_code import ResponseCode
25
6+ if TYPE_CHECKING :
7+ from hiero_sdk_python import (
8+ TransactionId ,
9+ TransactionReceipt
10+ )
11+
312class PrecheckError (Exception ):
413 """
514 Exception thrown when a transaction fails its precheck validation.
@@ -11,81 +20,97 @@ class PrecheckError(Exception):
1120 transaction_id (TransactionId): The ID of the transaction that failed.
1221 message (str): The message of the error. If not provided, a default message is generated.
1322 """
14- def __init__ (self , status , transaction_id = None , message = None ):
15- self .status = status
23+
24+ def __init__ (
25+ self ,
26+ status : Union [ResponseCode , int ],
27+ transaction_id : Optional [TransactionId ] = None ,
28+ message : Optional [str ] = None ,
29+ ) -> None :
30+ self .status = ResponseCode (status )
1631 self .transaction_id = transaction_id
17-
32+
1833 # Build a default message if none provided
1934 if message is None :
20- status_name = ResponseCode ( status ) .name
21- message = f"Transaction failed precheck with status: { status_name } ({ status } )"
35+ status_name = self . status .name
36+ message = f"Transaction failed precheck with status: { status_name } ({ int ( self . status ) } )"
2237 if transaction_id :
2338 message += f", transaction ID: { transaction_id } "
24-
39+
2540 self .message = message
2641 super ().__init__ (self .message )
27-
28- def __str__ (self ):
42+
43+ def __str__ (self ) -> str :
2944 return self .message
30-
31- def __repr__ (self ):
45+
46+ def __repr__ (self ) -> str :
3247 return f"PrecheckError(status={ self .status } , transaction_id={ self .transaction_id } )"
33-
34-
48+
49+
3550class MaxAttemptsError (Exception ):
3651 """
3752 Exception raised when the maximum number of attempts for a request has been reached.
38-
53+
3954 Attributes:
4055 message (str): The error message explaining why the maximum attempts were reached
4156 node_id (str): The ID of the node that was being contacted when the max attempts were reached
42- last_error (Exception ): The last error that occurred during the final attempt
57+ last_error (BaseException ): The last error that occurred during the final attempt
4358 """
44-
45- def __init__ (self , message , node_id , last_error = None ):
59+
60+ def __init__ (self , message : str , node_id : str , last_error : Optional [ BaseException ] = None ) -> None :
4661 self .node_id = node_id
4762 self .last_error = last_error
48-
63+
4964 # Build a comprehensive error message
5065 error_message = message
5166 if last_error is not None :
5267 error_message += f"; last error: { str (last_error )} "
53-
68+
5469 self .message = error_message
5570 super ().__init__ (self .message )
56-
57- def __str__ (self ):
71+
72+ def __str__ (self ) -> str :
5873 return self .message
59-
60- def __repr__ (self ):
74+
75+ def __repr__ (self ) -> str :
6176 return f"MaxAttemptsError(message='{ self .message } ', node_id='{ self .node_id } ')"
62-
77+
78+
6379class ReceiptStatusError (Exception ):
6480 """
6581 Exception raised when a transaction receipt contains an error status.
6682
6783 Attributes:
6884 status (ResponseCode): The error status code from the receipt
69- transaction_id (TransactionId): The ID of the transaction that failed
85+ transaction_id (TransactionId): The ID of the transaction that failed (Optional)
7086 transaction_receipt (TransactionReceipt): The receipt containing the error status
7187 message (str): The error message describing the failure
7288 """
7389
74- def __init__ (self , status , transaction_id , transaction_receipt , message = None ):
75- self .status = status
90+ def __init__ (
91+ self ,
92+ status : Union [ResponseCode , int ],
93+ transaction_id : Optional [TransactionId ],
94+ transaction_receipt : TransactionReceipt ,
95+ message : Optional [str ] = None
96+ ) -> None :
97+ self .status = ResponseCode (status )
7698 self .transaction_id = transaction_id
7799 self .transaction_receipt = transaction_receipt
78100
79101 # Build a default message if none provided
80102 if message is None :
81- status_name = ResponseCode (status ).name
82- message = f"Receipt for transaction { transaction_id } contained error status: { status_name } ({ status } )"
103+ status_name = self .status .name
104+ if transaction_id :
105+ message = f"Receipt for transaction { transaction_id } contained error status: { status_name } ({ int (self .status )} )"
106+ else :
107+ message = f"Receipt contained error status: { status_name } ({ int (self .status )} )"
83108
84109 self .message = message
85110 super ().__init__ (self .message )
86111
87- def __str__ (self ):
112+ def __str__ (self ) -> str :
88113 return self .message
89114
90- def __repr__ (self ):
91- return f"ReceiptStatusError(status={ self .status } , transaction_id={ self .transaction_id } )"
115+ def __repr__ (self ) -> str :
116+ return f"ReceiptStatusError(status={ self .status } , transaction_id={ self .transaction_id } )"
0 commit comments