Skip to content

Commit 24aec4c

Browse files
jahnvi480bewithgaurav
authored andcommitted
Merged PR 5308: Exception class modifications + Tests
#### AI description (iteration 1) #### PR Classification Code cleanup #### PR Summary Refactored exception handling to improve clarity and consistency in error messages. - Modified `mssql_python/exceptions.py` to update exception classes and their constructors. - Replaced the SQLSTATE to exception mapping with a function in `mssql_python/exceptions.py`. - Updated `mssql_python/helpers.py` to use the new `raise_exception` function for error handling. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot --> Related work items: #32946, #33799, #33805, #33806
1 parent 06e75ad commit 24aec4c

9 files changed

Lines changed: 591 additions & 498 deletions

File tree

mssql_python/connection.py

Lines changed: 53 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,14 @@ def _connect_to_db(self) -> None:
117117
"""
118118
if ENABLE_LOGGING:
119119
logging.info("Connecting to the database")
120-
try:
121-
ret = ddbc_bindings.DDBCSQLDriverConnect(
122-
self.hdbc.value, # Connection handle
123-
0, # Window handle
124-
self.connection_str # Connection string
125-
)
126-
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
127-
if ENABLE_LOGGING:
128-
logging.info("Connection established successfully.")
129-
except Exception as e:
130-
if ENABLE_LOGGING:
131-
logging.error("An error occurred while connecting to the database: %s", e)
132-
raise
120+
ret = ddbc_bindings.DDBCSQLDriverConnect(
121+
self.hdbc.value, # Connection handle
122+
0, # Window handle
123+
self.connection_str # Connection string
124+
)
125+
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
126+
if ENABLE_LOGGING:
127+
logging.info("Connection established successfully.")
133128

134129
@property
135130
def autocommit(self) -> bool:
@@ -138,16 +133,12 @@ def autocommit(self) -> bool:
138133
Returns:
139134
bool: True if autocommit is enabled, False otherwise.
140135
"""
141-
try:
142-
autocommit_mode = ddbc_bindings.DDBCSQLGetConnectionAttr(
143-
self.hdbc.value, # Connection handle
144-
odbc_sql_const.SQL_ATTR_AUTOCOMMIT.value, # Attribute
145-
)
146-
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, autocommit_mode)
147-
return autocommit_mode == odbc_sql_const.SQL_AUTOCOMMIT_ON.value
148-
except Exception as e:
149-
logging.error("An error occurred while getting autocommit mode: %s", e)
150-
raise Exception("DatabaseError: Failed to get autocommit mode") from e
136+
autocommit_mode = ddbc_bindings.DDBCSQLGetConnectionAttr(
137+
self.hdbc.value, # Connection handle
138+
odbc_sql_const.SQL_ATTR_AUTOCOMMIT.value, # Attribute
139+
)
140+
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, autocommit_mode)
141+
return autocommit_mode == odbc_sql_const.SQL_AUTOCOMMIT_ON.value
151142

152143
def setautocommit(self, value: bool) -> None:
153144
"""
@@ -159,22 +150,16 @@ def setautocommit(self, value: bool) -> None:
159150
Raises:
160151
DatabaseError: If there is an error while setting the autocommit mode.
161152
"""
162-
try:
163-
ret = ddbc_bindings.DDBCSQLSetConnectAttr(
164-
self.hdbc.value, # Connection handle
165-
odbc_sql_const.SQL_ATTR_AUTOCOMMIT.value, # Attribute
166-
odbc_sql_const.SQL_AUTOCOMMIT_ON.value if value else odbc_sql_const.SQL_AUTOCOMMIT_OFF.value, # Value
167-
0 # String length
168-
)
169-
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
170-
self._autocommit = value
171-
if ENABLE_LOGGING:
172-
logging.info("Autocommit mode set to %s.", value)
173-
except Exception as e:
174-
if ENABLE_LOGGING:
175-
logging.error("An error occurred while setting autocommit mode: %s", e)
176-
raise Exception("DatabaseError: Failed to set autocommit mode") from e
177-
153+
ret = ddbc_bindings.DDBCSQLSetConnectAttr(
154+
self.hdbc.value, # Connection handle
155+
odbc_sql_const.SQL_ATTR_AUTOCOMMIT.value, # Attribute
156+
odbc_sql_const.SQL_AUTOCOMMIT_ON.value if value else odbc_sql_const.SQL_AUTOCOMMIT_OFF.value, # Value
157+
0 # String length
158+
)
159+
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
160+
self._autocommit = value
161+
if ENABLE_LOGGING:
162+
logging.info("Autocommit mode set to %s.", value)
178163

179164
def cursor(self) -> Cursor:
180165
"""
@@ -191,11 +176,7 @@ def cursor(self) -> Cursor:
191176
DatabaseError: If there is an error while creating the cursor.
192177
InterfaceError: If there is an error related to the database interface.
193178
"""
194-
try:
195-
return Cursor(self)
196-
except Exception as e:
197-
logging.error("An error occurred while creating the cursor: %s", e)
198-
raise Exception("DatabaseError: Failed to create the cursor") from e
179+
return Cursor(self)
199180

200181
def commit(self) -> None:
201182
"""
@@ -209,20 +190,15 @@ def commit(self) -> None:
209190
Raises:
210191
DatabaseError: If there is an error while committing the transaction.
211192
"""
212-
try:
213-
# Commit the current transaction
214-
ret = ddbc_bindings.DDBCSQLEndTran(
215-
odbc_sql_const.SQL_HANDLE_DBC.value, # Handle type
216-
self.hdbc.value, # Connection handle
217-
odbc_sql_const.SQL_COMMIT.value # Commit the transaction
218-
)
219-
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
220-
if ENABLE_LOGGING:
221-
logging.info("Transaction committed successfully.")
222-
except Exception as e:
223-
if ENABLE_LOGGING:
224-
logging.error("An error occurred while committing the transaction: %s", e)
225-
raise Exception("DatabaseError: Failed to commit the transaction") from e
193+
# Commit the current transaction
194+
ret = ddbc_bindings.DDBCSQLEndTran(
195+
odbc_sql_const.SQL_HANDLE_DBC.value, # Handle type
196+
self.hdbc.value, # Connection handle
197+
odbc_sql_const.SQL_COMMIT.value # Commit the transaction
198+
)
199+
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
200+
if ENABLE_LOGGING:
201+
logging.info("Transaction committed successfully.")
226202

227203
def rollback(self) -> None:
228204
"""
@@ -235,20 +211,15 @@ def rollback(self) -> None:
235211
Raises:
236212
DatabaseError: If there is an error while rolling back the transaction.
237213
"""
238-
try:
239-
# Roll back the current transaction
240-
ret = ddbc_bindings.DDBCSQLEndTran(
241-
odbc_sql_const.SQL_HANDLE_DBC.value, # Handle type
242-
self.hdbc.value, # Connection handle
243-
odbc_sql_const.SQL_ROLLBACK.value # Roll back the transaction
244-
)
245-
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
246-
if ENABLE_LOGGING:
247-
logging.info("Transaction rolled back successfully.")
248-
except Exception as e:
249-
if ENABLE_LOGGING:
250-
logging.error("An error occurred while rolling back the transaction: %s", e)
251-
raise Exception("DatabaseError: Failed to roll back the transaction") from e
214+
# Roll back the current transaction
215+
ret = ddbc_bindings.DDBCSQLEndTran(
216+
odbc_sql_const.SQL_HANDLE_DBC.value, # Handle type
217+
self.hdbc.value, # Connection handle
218+
odbc_sql_const.SQL_ROLLBACK.value # Roll back the transaction
219+
)
220+
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
221+
if ENABLE_LOGGING:
222+
logging.info("Transaction rolled back successfully.")
252223

253224
def close(self) -> None:
254225
"""
@@ -263,18 +234,13 @@ def close(self) -> None:
263234
Raises:
264235
DatabaseError: If there is an error while closing the connection.
265236
"""
266-
try:
267-
# Disconnect from the database
268-
ret = ddbc_bindings.DDBCSQLDisconnect(self.hdbc.value)
269-
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
270-
271-
# Free the connection handle
272-
ret = ddbc_bindings.DDBCSQLFreeHandle(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value)
273-
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
274-
275-
if ENABLE_LOGGING:
276-
logging.info("Connection closed successfully.")
277-
except Exception as e:
278-
if ENABLE_LOGGING:
279-
logging.error("An error occurred while closing the connection: %s", e)
280-
raise Exception("DatabaseError: Failed to close the connection") from e
237+
# Disconnect from the database
238+
ret = ddbc_bindings.DDBCSQLDisconnect(self.hdbc.value)
239+
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
240+
241+
# Free the connection handle
242+
ret = ddbc_bindings.DDBCSQLFreeHandle(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value)
243+
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
244+
245+
if ENABLE_LOGGING:
246+
logging.info("Connection closed successfully.")

0 commit comments

Comments
 (0)