55import json
66import re
77
8+ # Constants for common error messages and codes
9+ DEFAULT_ERROR_MESSAGE = "An error occurred while processing the operation"
10+ DEFAULT_ERROR_CODE = "UnknownError"
11+
812
913class FabricCLIError (Exception ):
1014 def __init__ (self , message , status_code = None ):
@@ -70,8 +74,8 @@ def __init__(self, response_text):
7074 self .more_details : list [dict ] = response .get ("moreDetails" , [])
7175 self .request_id = response .get ("requestId" )
7276 except (json .JSONDecodeError , TypeError ):
73- message = "An error occurred while processing the operation"
74- error_code = "UnknownError"
77+ message = DEFAULT_ERROR_MESSAGE
78+ error_code = DEFAULT_ERROR_CODE
7579 self .more_details = []
7680 self .request_id = None
7781
@@ -116,15 +120,16 @@ def __init__(self, response_text):
116120 code (str): The error code returned by the API.
117121 message (str): A descriptive message about the error.
118122 """
123+ # Initialize properties before parsing
124+ self .request_id = None
125+ self .timestamp = None
126+
119127 try :
120128 response_data = json .loads (response_text ) if response_text else {}
121129 error_data = response_data .get ("error" , {})
122130 code = error_data .get ("code" )
123131 message = error_data .get ("message" )
124132
125- self .request_id = None
126- self .timestamp = None
127-
128133 if message :
129134 message = re .sub (r"\n(?=RequestId:)" , "" , message )
130135 match = re .search (r"RequestId:(\S+)" , message )
@@ -138,10 +143,8 @@ def __init__(self, response_text):
138143 self .timestamp = match .group (1 )
139144 message = message .replace (match .group (0 ), "" )
140145 except (json .JSONDecodeError , TypeError ):
141- message = "An error occurred while processing the operation"
142- code = "UnknownError"
143- self .request_id = None
144- self .timestamp = None
146+ message = DEFAULT_ERROR_MESSAGE
147+ code = DEFAULT_ERROR_CODE
145148
146149 super ().__init__ (message , code )
147150
@@ -198,6 +201,9 @@ def __init__(self, response_text):
198201 details (list): A list of additional error details, if available.
199202 additional_info (list): Additional info at the main error level, if available.
200203 """
204+ # Initialize properties before parsing
205+ self .request_id = None
206+
201207 try :
202208 response_data = json .loads (response_text ) if response_text else {}
203209 error_data = response_data .get ("error" , {})
@@ -206,16 +212,13 @@ def __init__(self, response_text):
206212
207213 details : list [dict ] = error_data .get ("details" , [])
208214
209- # Extract RootActivityId from the details
210- self .request_id = None
211215 for detail in details :
212216 if detail .get ("code" ) == "RootActivityId" :
213217 self .request_id = detail .get ("message" )
214218 break
215219 except (json .JSONDecodeError , TypeError ):
216- message = "An error occurred while processing the operation"
217- code = "UnknownError"
218- self .request_id = None
220+ message = DEFAULT_ERROR_MESSAGE
221+ code = DEFAULT_ERROR_CODE
219222
220223 super ().__init__ (message , code )
221224
0 commit comments