Skip to content

fix(source/mssql,source/cloudsqlmssql): include the server's messages in severe errors - #4445

Open
Mordris wants to merge 2 commits into
googleapis:mainfrom
Mordris:fix/mssql-severe-error-details
Open

Mordris wants to merge 2 commits into
googleapis:mainfrom
Mordris:fix/mssql-severe-error-details

Conversation

@Mordris

@Mordris Mordris commented Nov 7, 2026

Copy link
Copy Markdown

Description

For SQL Server errors of severity 20 and above, the mssql and cloud-sql-mssql sources answer with only SQL Server had internal error: no error number, severity or message (#4444).

go-mssqldb returns mssql.ServerError for these errors. Its Error() is that fixed text; the errors the server sent are kept in the mssql.Error it wraps. That error's own fields describe the last error, which is usually a consequence ("the session is in the kill state"); its All field lists every error from first to last, and the first is the cause.

This PR adds mssqlcommon.WithServerErrorDetails, which RunSQL in both sources now applies at its two error returns (the query call and results.Err()). For an mssql.ServerError it appends every entry of All, in order, formatted from its fields, after the existing text, and keeps the original error wrapped, so errors.As still finds it. Every other error is returned unchanged.

RAISERROR('severe error text', 20, 1) WITH LOG through mssql-execute-sql, before and after (SQL Server 2025 CU10):

before: error processing request: unable to execute query: SQL Server had internal error
after:  error processing request: unable to execute query: SQL Server had internal error: severe error text (error 50000, severity 20, state 1); Process ID 89 has raised user error 50000, severity 20. SQL Server is terminating this process. (error 2745, severity 16, state 2); Cannot continue the execution because the session is in the kill state. (error 596, severity 21, state 1)

Design notes:

Testing:

  • Unit tests for the helper in internal/tools/mssql/mssqlcommon/errors_test.go. mssql.ServerError keeps the wrapped error in an unexported field, so the tests stand in for it with errors.Join(mssql.Error{...}, mssql.ServerError{}).
  • The Linux job of tests.yaml (build, both go test -race runs, coverage gate), the go mod tidy check, and golangci-lint v2.15.0 on the changed packages (0 issues), all in a golang:1.27.3 container.
  • Coverage: the new mssqlcommon code is at 100%. internal/sources/mssql (8.2%) and internal/sources/cloudsqlmssql (7.9%) are the same as on main; RunSQL, where the two call sites are, is reached only by the integration tests.
  • Live, with Toolbox built from this branch with the repository's Dockerfile, against SQL Server 2025 CU10: both error returns (the query call above, and results.Err() via SELECT 1 AS a; RAISERROR(...)) now carry the chain. Severity 16 errors, divide by zero, result sets and multiple result sets are unchanged. The chain the helper formats is the same on SQL Server 2017 CU31, 2019 CU32, 2022 CU27 and 2025 CU10, with go-mssqldb v1.10.0 and v1.11.2 (details in mssql: severe errors (severity 20 and above) lose the server's message: "SQL Server had internal error" #4444).
  • No integration test. Raising severity 19 and above WITH LOG needs a login in the sysadmin role or with ALTER TRACE (a plain login gets error 2778), and I don't know whether the CI logins have either. If they do, I can add one to tests/mssql.
  • cloud-sql-mssql gets the same change at the same two returns; I couldn't run it against Cloud SQL.

Security:

  • No query is built or changed, and no user input reaches SQL.
  • The added text is SQL Server's own error messages, which Toolbox already returns for errors below severity 20. No credentials, connection strings or parameters are added.
  • No file paths, commands or auth checks are touched.

PR Checklist

  • Make sure you reviewed
    CONTRIBUTING.md
  • Make sure to open an issue as a
    bug/issue
    before writing your code! That way we can discuss the change, evaluate
    designs, and agree on the general idea
  • Ensure you have manually reviewed the entire diff before requesting a
    review
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary): no docs change needed
  • Make sure to add ! if this involve a breaking change: not breaking; the existing text stays as the prefix

🛠️ Fixes #4444

… in severe errors

For severity 20 and above, go-mssqldb returns mssql.ServerError, whose
Error() is the fixed "SQL Server had internal error". The errors the
server sent are kept in the mssql.Error it wraps: its own fields describe
the last one, and its All field lists every error from first to last.

RunSQL in both sources now appends every entry of All, formatted from its
fields, after the existing text, and keeps the original error wrapped.
Other errors are unchanged.

Fixes googleapis#4444
@Mordris
Mordris requested review from a team as code owners November 7, 2026 09:55

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a common error-handling utility for MSSQL sources to extract and format detailed SQL Server errors from severe database errors, and integrates it into both the Cloud SQL MSSQL and standard MSSQL sources. Feedback on the changes suggests checking the return value of errors.As when extracting the underlying mssql.Error to prevent passing an uninitialized zero-value struct to the formatting function.

…lt for the wrapped mssql.Error

Look up mssql.ServerError and the mssql.Error it wraps in one condition
and return the error unchanged if either is missing, instead of relying
on an empty mssql.Error formatting to "".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mssql: severe errors (severity 20 and above) lose the server's message: "SQL Server had internal error"

2 participants