See More

#region License // The PostgreSQL License // // Copyright (C) 2015 The Npgsql Development Team // // Permission to use, copy, modify, and distribute this software and its // documentation for any purpose, without fee, and without a written // agreement is hereby granted, provided that the above copyright notice // and this paragraph and the following two paragraphs appear in all copies. // // IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY // FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS // DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF // THE POSSIBILITY OF SUCH DAMAGE. // // THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES, // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS // ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. #endregion using System; using System.Collections; using System.Collections.Generic; using System.Data.Common; using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; using JetBrains.Annotations; using Npgsql.BackendMessages; #if NET45 || NET451 || DNX451 using System.Runtime.Serialization; #endif namespace Npgsql { ///

/// The exception that is thrown when the PostgreSQL backend reports errors. /// Note that other errors (network issues, Npgsql client-side problems) are reported as regular /// .NET exceptions. /// /// /// See http://www.postgresql.org/docs/current/static/errcodes-appendix.html, /// http://www.postgresql.org/docs/current/static/protocol-error-fields.html /// #if NET45 || NET451 || DNX451 [Serializable] #endif public sealed class NpgsqlException : DbException { readonly ErrorOrNoticeMessage _msg; Dictionary _data; #region Message Fields /// /// Severity of the error or notice. /// Always present. /// [PublicAPI] public string Severity => _msg.Severity; /// /// The SQLSTATE code for the error. /// /// /// Always present. /// See http://www.postgresql.org/docs/current/static/errcodes-appendix.html /// [PublicAPI] public string Code => _msg.Code; /// /// The primary human-readable error message. This should be accurate but terse. /// /// /// Always present. /// [PublicAPI] public string MessageText => _msg.Message; /// /// An optional secondary error message carrying more detail about the problem. /// May run to multiple lines. /// [PublicAPI] public string Detail => _msg.Detail; /// /// An optional suggestion what to do about the problem. /// This is intended to differ from Detail in that it offers advice (potentially inappropriate) rather than hard facts. /// May run to multiple lines. /// [PublicAPI] public string Hint => _msg.Hint; /// /// The field value is a decimal ASCII integer, indicating an error cursor position as an index into the original query string. /// The first character has index 1, and positions are measured in characters not bytes. /// 0 means not provided. /// [PublicAPI] public int Position => _msg.Position; /// /// This is defined the same as the field, but it is used when the cursor position refers to an internally generated command rather than the one submitted by the client. /// The field will always appear when this field appears. /// 0 means not provided. /// [PublicAPI] public int InternalPosition => _msg.InternalPosition; /// /// The text of a failed internally-generated command. /// This could be, for example, a SQL query issued by a PL/pgSQL function. /// [PublicAPI] public string InternalQuery => _msg.InternalQuery; /// /// An indication of the context in which the error occurred. /// Presently this includes a call stack traceback of active PL functions. /// The trace is one entry per line, most recent first. /// [PublicAPI] public string Where => _msg.Where; /// /// If the error was associated with a specific database object, the name of the schema containing that object, if any. /// /// PostgreSQL 9.3 and up. [PublicAPI] public string SchemaName => _msg.SchemaName; /// /// Table name: if the error was associated with a specific table, the name of the table. /// (Refer to the schema name field for the name of the table's schema.) /// /// PostgreSQL 9.3 and up. [PublicAPI] public string TableName => _msg.TableName; /// /// If the error was associated with a specific table column, the name of the column. /// (Refer to the schema and table name fields to identify the table.) /// /// PostgreSQL 9.3 and up. [PublicAPI] public string ColumnName => _msg.ColumnName; /// /// If the error was associated with a specific data type, the name of the data type. /// (Refer to the schema name field for the name of the data type's schema.) /// /// PostgreSQL 9.3 and up. [PublicAPI] public string DataTypeName => _msg.DataTypeName; /// /// If the error was associated with a specific constraint, the name of the constraint. /// Refer to fields listed above for the associated table or domain. /// (For this purpose, indexes are treated as constraints, even if they weren't created with constraint syntax.) /// /// PostgreSQL 9.3 and up. [PublicAPI] public string ConstraintName => _msg.ConstraintName; /// /// The file name of the source-code location where the error was reported. /// /// PostgreSQL 9.3 and up. [PublicAPI] public string File => _msg.File; /// /// The line number of the source-code location where the error was reported. /// [PublicAPI] public string Line => _msg.Line; /// /// The name of the source-code routine reporting the error. /// [PublicAPI] public string Routine => _msg.Routine; /// /// Same as , for backwards-compatibility with Npgsql 2.x /// [Obsolete("Use MessageText instead")] [PublicAPI] public string BaseMessage => _msg.Message; #endregion internal NpgsqlException(NpgsqlBuffer buf) { _msg = new ErrorOrNoticeMessage(buf); } /// /// Gets a the PostgreSQL error message and code. /// public override string Message => Code + ": " + MessageText; /// /// Gets a collection of key/value pairs that provide additional PostgreSQL fields about the exception. /// public override IDictionary Data { get { return _data ?? (_data = ( from p in typeof (ErrorOrNoticeMessage).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance) let k = p.Name let v = p.GetValue(_msg) where p.GetValue(_msg) != null where (k != "Position" && k != "InternalPosition") || ((int)v) != 0 select new {Key = k, Value = v} ).ToDictionary(kv => kv.Key, kv => kv.Value) ); } } #region Serialization #if NET45 || NET451 || DNX451 NpgsqlException(SerializationInfo info, StreamingContext context) : base(info, context) { _msg = (ErrorOrNoticeMessage)info.GetValue("msg", typeof(ErrorOrNoticeMessage)); } /// /// Populates a with the data needed to serialize the target object. /// /// The to populate with data. /// The destination (see ) for this serialization. public override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); info.AddValue("msg", _msg); } #endif #endregion } }