Repository navigation
fix: keep the Msg/Level/State header on errors sent to stderr with -r - #811
Closed
Raymondriter wants to merge 1 commit into
Closed
Raymondriter wants to merge 1 commit into
Raymondriter wants to merge 1 commit into
Conversation
With -r, runQuery handed the PrintError hook only e.Message, and the hook's `true` return skipped Formatter.AddError, which is where the "Msg N, Level N, State N, Server S, Line N" header is built. Errors on stderr therefore lost the error number and line, unlike ODBC sqlcmd, where -r only changes the destination. Move the header formatting into errorHeader() and pass header+message to PrintError, so a redirecting host gets the same text the default output shows. The PrintError signature is unchanged. Fixes microsoft#794 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Redirected errors omit the mssql: prefix when -r is combined with -j.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Fixes redirected SQL Server errors so -r output retains the standard diagnostic header.
Changes:
- Centralizes localized error-header formatting.
- Includes headers in redirected error messages.
- Adds integration coverage and documents the
PrintErrorcontract.
File summaries
| File | Summary |
|---|---|
pkg/sqlcmd/sqlcmd.go |
Updates redirected error handling and documentation. |
pkg/sqlcmd/sqlcmd_test.go |
Tests redirected error headers. |
pkg/sqlcmd/format.go |
Centralizes server error header formatting. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| switch e := m.Error.(type) { | ||
| case mssql.Error: | ||
| if !s.PrintError(e.Message, e.Class) { | ||
| if !s.PrintError(errorHeader(e)+e.Message, e.Class) { |
Author
|
Withdrawing this for now. The root cause and fix are described above in case they're useful to anyone picking up #794. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #794
Problem
With
-r,runQuerypassed onlye.Messageto thePrintErrorhook. Because the hook returnstrue,Formatter.AddErrorwas skipped, and that's where theMsg N, Level N, State N, Server S, Line Nheader is built. Errors on stderr lost the error number and line. ODBC sqlcmd prints the same text with-rand only changes the destination.Fix
AddErrorintoerrorHeader(e mssql.Error). The localized format strings are unchanged, so existing translations still apply.errorHeader(e) + e.MessagetoPrintError, so a host that redirects errors gets the text the default output would show.PrintErrorsignature is unchanged. I documented on the field thatmsgincludes the header for server errors.Tests
New
TestPrintErrorIncludesErrorHeaderrunsRAISERROR (N'Testing!', 11, 1)with a redirecting hook. It asserts the hook receivesMsg 50000, Level 11, State 1, Server ..., Line 1followed by the message, and that nothing reaches the default output. Onmainthe hook receives onlyTesting!.Full suite against a local SQL Server 2022 container: the same results as
main. The only failures are the environment-dependent install/ADS/editor tests.End to end with the repro from the issue (
sqlcmd -r -b -i repro.sql), stderr now reads:This matches the output without
-r.Not addressed here: the issue's additional observation that
-bstops after the first error of a batch. It's a separate behaviour change, and I can follow up in its own PR.🤖 Generated with Claude Code