FEAT: Expose SQLSTATE (and native error number) as attributes on exception objects
Maintainer thường phản hồi trong vòng 1 ngày
@gargsaumya đang làm issue này rồi.
Từ ngày 15/5/2026.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Summary
mssql_python discards the SQLSTATE code after using it to choose an exception subclass, so callers cannot programmatically classify errors by SQLSTATE. The only stable hook left is substring-matching the English driver_error text, which is brittle and forces every consumer to duplicate the driver's internal SQLSTATE → text table.
Please expose SQLSTATE as a public attribute on the exception (and ideally also the native SQL Server error number).
Why this matters
SQLSTATE is the one stable, standardized, locale-independent error identifier in ODBC. It's exactly what application code needs to decide things like:
- Is this transient? Retry it. (e.g.
HYT00,08S01,40001) - Is this a deadlock specifically vs. some other operational error? (
40001) - Is this an Azure SQL throttling response? (engine 40501 under
42000) - Is this an auth failure that means "refresh the access token"? (
28000)
Today none of that is possible without one of two bad options:
- Substring-match
driver_erroragainst a frozenset of English phrases copied out ofexceptions.py. This pins user code to internal driver constants, breaks if the strings are ever reworded or localized, and forces every consumer to maintain their own copy of the SQLSTATE-to-phrase mapping. - Regex-scrape
ddbc_errorforError NNNNto recover the SQL Server engine number. Fragile and explicitly not a stable contract.
Compare to peer drivers:
| Driver | Stable error identifier |
|---|---|
| pyodbc | exc.args[0] is the 5-char SQLSTATE |
| pymssql | exc.args[0] is the SQL Server engine error number |
| mssql-python | Neither; SQLSTATE is consumed and discarded in sqlstate_to_exception() |
PEP 249 doesn't standardize this, but every mature driver exposes something. mssql-python is currently the odd one out.
Proposed change
In mssql_python/exceptions.py:
- Add
sqlstate(and optionallynative_error) as keyword args toException.__init__and store them onself. - Update
sqlstate_to_exception(sqlstate, ddbc_error)to passsqlstate=sqlstateinto every constructor call (it already has the value in hand). - Update the
DatabaseErrorfallback inraise_exception()to pass the SQLSTATE through as well.
Roughly:
class Exception(builtins.Exception):
def __init__(self, driver_error: str, ddbc_error: str,
sqlstate: Optional[str] = None,
native_error: Optional[int] = None) -> None:
self.driver_error = driver_error
self.ddbc_error = truncate_error_message(ddbc_error)
self.sqlstate = sqlstate
self.native_error = native_error
...
This is fully backward compatible: existing callers that only read driver_error / ddbc_error / str(exc) keep working unchanged.
How callers would use it
TRANSIENT_SQLSTATES = frozenset({
"HYT00", "HYT01", # Timeouts
"08001", "08S01", "08007", # Connection failures
"40001", "40003", # Deadlock / indeterminate completion
})
def is_transient(exc: BaseException) -> bool:
return getattr(exc, "sqlstate", None) in TRANSIENT_SQLSTATES
This is what retry-logic samples in the official SQL Server docs would prefer to show, instead of the current English-text frozenset.
Related
PR #562 (fix for #532) is already plumbing SQLSTATE through Connection::checkError() into sqlstate_to_exception(). The value is now reaching the exception constructor from both the cursor path and the connection path. Exposing it on self is the natural next step and is essentially free once #562 lands.
Environment
mssql-python1.5.0- Verified empirically:
dir(exc)onOperationalError,ProgrammingError,DataErrorshows nosqlstate,native_error,errno, orcodeattribute.
- Ngôn ngữ chính
- Python
- Star
- 478
- Fork
- 62
- Merge trung bình
- 3 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 27
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/mssql-python
-
area: bulk-copy
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
microsoft/mssql-python#843 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
macOS: encrypted connections fail after Homebrew moved the `openssl` alias to `openssl@4`Có thể đã có người làm @bewithgaurav đã nhận 14 ngày trước. Đang mởarea: packaging-platform bug triage needed
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
microsoft/mssql-python#826 · 2 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bulkcopy PyCore login is closed by SQL Server 2019 while ODBC and Go clients succeed (1.14.0)Có thể làm lại được @bewithgaurav đã nhận 34 ngày trước và không có pull request nào đang mở. Đang mởarea: bulk-copy bug triage done
microsoft/mssql-python#750 · 4 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Announcement: mssql-python is coming to Django through mssql-django 2.0Có thể làm lại được @bewithgaurav đã nhận 41 ngày trước và không có pull request nào đang mở. Đang mởannouncement
microsoft/mssql-python#748 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Connection pool can briefly exceed max_size when pooling is disabled under loadCó thể đã có người làm @bewithgaurav đã nhận 23 ngày trước. Đang mởarea: connectivity-auth bug triage done under development
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 62/100
microsoft/mssql-python#746 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của microsoft/mssql-python
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
-
[Bug]: Importing weave changes the process-wide localeCó thể đã có người làm @himanshu748 đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
UX
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
mediajunkie/piper-morgan-product#2008 · 2 bình luận ·