question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SqlException Includes PRINTed Text - Bug?

See original GitHub issue

Using SqlClient, if a SQL statement uses PRINT to output text then encounters an error, the exception thrown by SqlClient may contain both the error returned by SQL Server (expected) and the output from the print PRINT statement (not expected).

For obvious reasons, it makes sense that the exception contains the error. 😃 However, the print statement’s contents are not an error and do not necessarily have any connection with the error. Including PRINT’s output in the exception seems incorrect.

Is this a bug?

Instead, shouldn’t the application subscribe to the connection’s InfoMessage (ref2) event if they want PRINT statement output and the exception be limited to just true error message content?

Example

Code

Target Framework: netcoreapp2.2 Package Reference: System.Data.SqlClient, version 4.6.1

using System;
using System.Data.SqlClient;

namespace ErrorExample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var connection = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=true")) {
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "PRINT 'hello world!'; THROW 50001, N'Ouch', 1";
                    command.ExecuteNonQuery();
                }
            }
        }
    }
}

Output

System.Data.SqlClient.SqlException: 'Ouch
hello world!'

TDS

At the database protocol-level, the PRINT message is returned in an info token separately from the error message, which is returned in an error token:

  • Info Token - ErrNo: 0, State: 1, Class: 0, Message: 'hello world!', Server Name: [server name]\[instance], ProcName: ''

  • Error Token - ErrNo: 50001, State: 1, Class: 16, Message: 'Ouch', Server Name: [server name]\[instance], ProcName: ''

So, the tokens returned by the server have the two messages properly segmented between them. The problem is client-side, where the two tokens are being aggregated into a single exception.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
karinazhoucommented, Jun 18, 2019

@bgribaudo I am able to reproduce this with Microsoft.Data.SqlClient as well. We will investigate more into this. Thank you for reporting it!

0reactions
bgribaudocommented, Jul 12, 2019

Thank you, @karinazhou!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Obtain the Query/CommandText that caused a SQLException
When we log SQL exceptions it'd be super useful if we could see the actual query that caused the exception. Is there a...
Read more >
SqlException Class (System.Data.SqlClient)
The exception that is thrown when SQL Server returns a warning or error. This class cannot be inherited.
Read more >
Handling SQLExceptions - JDBC Basics
The SQLException instance contains the following information that can help you determine the cause of the error: A description of the error.
Read more >
How to implement error handling in SQL Server
This article explains how to implement SQL error handling using the SQL server try-catch statement and what can be done in general when...
Read more >
SQL error messages and exceptions
01J12, Unable to obtain message text from server. See the next exception. ... 08006, A communications error has been detected: <error> .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found