|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | + |
| 7 | +namespace Microsoft.Data.SqlClient |
| 8 | +{ |
| 9 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/SqlError/*' /> |
| 10 | + [Serializable] |
| 11 | + public sealed class SqlError |
| 12 | + { |
| 13 | + // bug fix - MDAC 48965 - missing source of exception |
| 14 | + private readonly string _source = TdsEnums.SQL_PROVIDER_NAME; |
| 15 | + private readonly int _number; |
| 16 | + private readonly byte _state; |
| 17 | + private readonly byte _errorClass; |
| 18 | + [System.Runtime.Serialization.OptionalField(VersionAdded = 2)] |
| 19 | + private readonly string _server; |
| 20 | + private readonly string _message; |
| 21 | + private readonly string _procedure; |
| 22 | + private readonly int _lineNumber; |
| 23 | + [System.Runtime.Serialization.OptionalField(VersionAdded = 4)] |
| 24 | + private readonly int _win32ErrorCode; |
| 25 | + [System.Runtime.Serialization.OptionalField(VersionAdded = 5)] |
| 26 | + private readonly Exception _exception; |
| 27 | + |
| 28 | + internal SqlError(int infoNumber, byte errorState, byte errorClass, string server, string errorMessage, string procedure, int lineNumber, uint win32ErrorCode, Exception exception = null) |
| 29 | + : this(infoNumber, errorState, errorClass, server, errorMessage, procedure, lineNumber, exception) |
| 30 | + { |
| 31 | + _server = server; |
| 32 | + _win32ErrorCode = (int)win32ErrorCode; |
| 33 | + } |
| 34 | + |
| 35 | + internal SqlError(int infoNumber, byte errorState, byte errorClass, string server, string errorMessage, string procedure, int lineNumber, Exception exception = null) |
| 36 | + { |
| 37 | + _number = infoNumber; |
| 38 | + _state = errorState; |
| 39 | + _errorClass = errorClass; |
| 40 | + _server = server; |
| 41 | + _message = errorMessage; |
| 42 | + _procedure = procedure; |
| 43 | + _lineNumber = lineNumber; |
| 44 | + _win32ErrorCode = 0; |
| 45 | + _exception = exception; |
| 46 | + if (errorClass != 0) |
| 47 | + { |
| 48 | + SqlClientEventSource.Log.TryTraceEvent("SqlError.ctor | ERR | Info Number {0}, Error State {1}, Error Class {2}, Error Message '{3}', Procedure '{4}', Line Number {5}", infoNumber, (int)errorState, (int)errorClass, errorMessage, procedure ?? "None", (int)lineNumber); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/ToString/*' /> |
| 53 | + // bug fix - MDAC #49280 - SqlError does not implement ToString(); |
| 54 | + // There is no exception stack included because the correct exception stack is only available |
| 55 | + // on SqlException, and to obtain that the SqlError would have to have backpointers all the |
| 56 | + // way back to SqlException. If the user needs a call stack, they can obtain it on SqlException. |
| 57 | + public override string ToString() |
| 58 | + { |
| 59 | + return typeof(SqlError).ToString() + ": " + Message; // since this is sealed so we can change GetType to typeof |
| 60 | + } |
| 61 | + |
| 62 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Source/*' /> |
| 63 | + // bug fix - MDAC #48965 - missing source of exception |
| 64 | + public string Source => _source; |
| 65 | + |
| 66 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Number/*' /> |
| 67 | + public int Number => _number; |
| 68 | + |
| 69 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/State/*' /> |
| 70 | + public byte State => _state; |
| 71 | + |
| 72 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Class/*' /> |
| 73 | + public byte Class => _errorClass; |
| 74 | + |
| 75 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Server/*' /> |
| 76 | + public string Server => _server; |
| 77 | + |
| 78 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Message/*' /> |
| 79 | + public string Message => _message; |
| 80 | + |
| 81 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/Procedure/*' /> |
| 82 | + public string Procedure => _procedure; |
| 83 | + |
| 84 | + /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlError.xml' path='docs/members[@name="SqlError"]/LineNumber/*' /> |
| 85 | + public int LineNumber => _lineNumber; |
| 86 | + |
| 87 | + internal int Win32ErrorCode => _win32ErrorCode; |
| 88 | + |
| 89 | + internal Exception Exception => _exception; |
| 90 | + } |
| 91 | +} |
0 commit comments