Skip to content

Conversation

jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Aug 5, 2025

Work Item / Issue Reference

AB#34889


Summary

This pull request enhances DB-API 2.0 compliance for exception handling in the mssql_python package. The main changes are the addition of all standard DB-API 2.0 exception classes as attributes on the Connection class, refactoring error handling in the Cursor class to use these exceptions, and introducing comprehensive tests to verify correct behavior, inheritance, and consistency of these exception attributes.

DB-API 2.0 Exception Support

  • Added all DB-API 2.0 exception classes (Warning, Error, InterfaceError, DatabaseError, DataError, OperationalError, IntegrityError, InternalError, ProgrammingError, NotSupportedError) as attributes on the Connection class, making it possible to catch exceptions using connection.Error, connection.ProgrammingError, etc. (mssql_python/connection.py)
    Error Handling Improvements

  • Updated the Cursor class to raise InterfaceError (instead of generic Exception) when operations are attempted on a closed cursor, ensuring proper use of DB-API exceptions. (mssql_python/cursor.py)

Testing Enhancements

  • Added extensive tests to verify:
    • Presence and correctness of exception attributes on Connection instances and the class itself.
    • Proper inheritance hierarchy of exception classes.
    • Instantiation and catching of exceptions via connection attributes.
    • Consistency of exception attributes across multiple connections.
    • Real-world error handling scenarios using these attributes.
    • Correct error raising and handling when operating on closed cursors. (tests/test_003_connection.py)

@Copilot Copilot AI review requested due to automatic review settings August 5, 2025 05:08
@github-actions github-actions bot added the pr-size: medium Moderate update size label Aug 5, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances DB-API 2.0 compliance by adding exception handling attributes to the Connection class and improving error handling in the Cursor class. The changes enable users to catch exceptions using connection-based attributes like connection.Error and connection.ProgrammingError as required by the DB-API 2.0 specification.

  • Added all standard DB-API 2.0 exception classes as attributes on the Connection class
  • Updated cursor error handling to raise InterfaceError instead of generic Exception for closed cursor operations
  • Added comprehensive tests covering exception attributes, inheritance, instantiation, and real-world error scenarios

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
mssql_python/connection.py Imports all DB-API 2.0 exception classes and adds them as class attributes to enable connection-based exception handling
mssql_python/cursor.py Updates error handling in cursor close operations to raise InterfaceError instead of generic Exception
tests/test_003_connection.py Adds extensive test coverage for exception attributes, inheritance hierarchy, instantiation, and error handling scenarios
Comments suppressed due to low confidence (2)

tests/test_003_connection.py:290

  • [nitpick] The variable name warning conflicts with Python's built-in warning module. Consider renaming it to warning_instance or test_warning to avoid potential confusion.
    warning = db_connection.Warning("Test warning", "DDBC warning")

tests/test_003_connection.py:294

  • [nitpick] The variable name error is too generic and could be confusing in an error handling context. Consider renaming it to error_instance or test_error for clarity.
    error = db_connection.Error("Test error", "DDBC error")

bewithgaurav
bewithgaurav previously approved these changes Aug 5, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Aug 13, 2025
Copy link
Contributor

@sumitmsft sumitmsft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left few comments.. Please resolve them

@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Aug 25, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Aug 25, 2025
@jahnvi480 jahnvi480 merged commit 23a6d44 into main Aug 27, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-size: medium Moderate update size
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants