Skip to content

FIX: GH-Issue (#532) - Login failures are raised as RuntimeError instead of a mssql_python exception - #562

Merged
gargsaumya merged 7 commits into
mainfrom
saumya/gh-532
May 15, 2026
Merged

gargsaumya merged 7 commits into
mainfrom
saumya/gh-532

Conversation

@gargsaumya

@gargsaumya gargsaumya commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

Work Item / Issue Reference

AB#44632

GitHub Issue: #532


Summary

This pull request improves error handling in the mssql_python connection layer by ensuring that low-level runtime errors from the C++ pybind layer are consistently mapped to the correct DB-API 2.0 exceptions using SQLSTATE codes. This makes error reporting more robust and predictable, especially during connection, commit, and rollback operations. The changes also add comprehensive tests to verify this behavior.

Error handling improvements:

  • Introduced the _raise_connection_error function in connection.py to parse RuntimeError messages from the C++ layer, extract SQLSTATE codes, and map them to appropriate DB-API exceptions using sqlstate_to_exception. If no SQLSTATE is found, a fallback OperationalError is raised. ([mssql_python/connection.pyR61-R89](https://github.com/microsoft/mssql-python/pull/562/files#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R61-R89))
  • Updated the connection initialization, setautocommit, commit, and rollback methods in connection.py to wrap calls to the native layer in try/except blocks that utilize _raise_connection_error, ensuring consistent exception mapping throughout the connection lifecycle. ([[1]](https://github.com/microsoft/mssql-python/pull/562/files#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R362-R367), [[2]](https://github.com/microsoft/mssql-python/pull/562/files#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R528-R531), [[3]](https://github.com/microsoft/mssql-python/pull/562/files#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R1516-R1519), [[4]](https://github.com/microsoft/mssql-python/pull/562/files#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R1542-R1545))
  • Modified the C++ Connection::checkError method to format error messages as "SQLSTATE:XXXXX:<odbc_error_message>", enabling the Python layer to parse and handle errors correctly. ([mssql_python/pybind/connection/connection.cppR182-R186](https://github.com/microsoft/mssql-python/pull/562/files#diff-eca696c13d997f510e5f9b16288ed1deb0ad132768c283eda1518f78edf9b6ecR182-R186))

Testing enhancements:

  • Added a regression test (test_connect_runtime_error_mapped_to_correct_dbapi_exception) to verify that connection, commit, and rollback errors from the C++ layer are mapped to the correct DB-API exceptions, including handling of unknown or missing SQLSTATE codes. ([tests/test_006_exceptions.pyR193-R257](https://github.com/microsoft/mssql-python/pull/562/files#diff-16f96f3facfc774c12b49a09116108763139a8a92ceef526fbb4f4f01600c6fdR193-R257))

Dependency update:

  • Imported sqlstate_to_exception in connection.py to support the new error mapping logic. ([mssql_python/connection.pyR41](https://github.com/microsoft/mssql-python/pull/562/files#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R41))