forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresException.cs
More file actions
351 lines (320 loc) · 14.3 KB
/
PostgresException.cs
File metadata and controls
351 lines (320 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#region License
// The PostgreSQL License
//
// Copyright (C) 2017 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.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Npgsql.BackendMessages;
#if !NETSTANDARD1_3
using System.Runtime.Serialization;
#endif
#pragma warning disable CA1032
namespace Npgsql
{
/// <summary>
/// The exception that is thrown when the PostgreSQL backend reports errors (e.g. query
/// SQL issues, constraint violations).
/// </summary>
/// <remarks>
/// This exception only corresponds to a PostgreSQL-delivered error.
/// Other errors (e.g. network issues) will be raised via <see cref="NpgsqlException"/>,
/// and purely Npgsql-related issues which aren't related to the server will be raised
/// via the standard CLR exceptions (e.g. ArgumentException).
///
/// See http://www.postgresql.org/docs/current/static/errcodes-appendix.html,
/// http://www.postgresql.org/docs/current/static/protocol-error-fields.html
/// </remarks>
#if !NETSTANDARD1_3
[Serializable]
#endif
public sealed class PostgresException : NpgsqlException
{
[CanBeNull]
Dictionary<string, object> _data;
#region Message Fields
/// <summary>
/// Severity of the error or notice.
/// Always present.
/// </summary>
[PublicAPI]
public string Severity { get; set; }
/// <summary>
/// The SQLSTATE code for the error.
/// </summary>
/// <remarks>
/// Always present.
/// See http://www.postgresql.org/docs/current/static/errcodes-appendix.html
/// </remarks>
[PublicAPI]
public string SqlState { get; set; }
/// <summary>
/// The SQLSTATE code for the error.
/// </summary>
/// <remarks>
/// Always present.
/// See http://www.postgresql.org/docs/current/static/errcodes-appendix.html
/// </remarks>
[PublicAPI, Obsolete("Use SqlState instead")]
public string Code => SqlState;
/// <summary>
/// The primary human-readable error message. This should be accurate but terse.
/// </summary>
/// <remarks>
/// Always present.
/// </remarks>
[PublicAPI]
public string MessageText { get; set; }
/// <summary>
/// An optional secondary error message carrying more detail about the problem.
/// May run to multiple lines.
/// </summary>
[PublicAPI]
public string Detail { get; set; }
/// <summary>
/// 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.
/// </summary>
[PublicAPI]
public string Hint { get; set; }
/// <summary>
/// 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.
/// </summary>
[PublicAPI]
public int Position { get; set; }
/// <summary>
/// This is defined the same as the <see cref="Position"/> field, but it is used when the cursor position refers to an internally generated command rather than the one submitted by the client.
/// The <see cref="InternalQuery" /> field will always appear when this field appears.
/// 0 means not provided.
/// </summary>
[PublicAPI]
public int InternalPosition { get; set; }
/// <summary>
/// The text of a failed internally-generated command.
/// This could be, for example, a SQL query issued by a PL/pgSQL function.
/// </summary>
[PublicAPI]
public string InternalQuery { get; set; }
/// <summary>
/// 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.
/// </summary>
[PublicAPI]
public string Where { get; set; }
/// <summary>
/// If the error was associated with a specific database object, the name of the schema containing that object, if any.
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string SchemaName { get; set; }
/// <summary>
/// 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.)
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string TableName { get; set; }
/// <summary>
/// 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.)
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string ColumnName { get; set; }
/// <summary>
/// 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.)
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string DataTypeName { get; set; }
/// <summary>
/// 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.)
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string ConstraintName { get; set; }
/// <summary>
/// The file name of the source-code location where the error was reported.
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string File { get; set; }
/// <summary>
/// The line number of the source-code location where the error was reported.
/// </summary>
[PublicAPI]
public string Line { get; set; }
/// <summary>
/// The name of the source-code routine reporting the error.
/// </summary>
[PublicAPI]
public string Routine { get; set; }
#endregion
/// <summary>
/// Creates a new instance.
/// </summary>
public PostgresException() {}
internal PostgresException(NpgsqlReadBuffer buf)
{
var msg = new ErrorOrNoticeMessage(buf);
Severity = msg.Severity;
SqlState = msg.Code;
MessageText = msg.Message;
Detail = msg.Detail;
Hint = msg.Hint;
Position = msg.Position;
InternalPosition = msg.InternalPosition;
InternalQuery = msg.InternalQuery;
Where = msg.Where;
SchemaName = msg.SchemaName;
TableName = msg.TableName;
ColumnName = msg.ColumnName;
DataTypeName = msg.DataTypeName;
ConstraintName = msg.ConstraintName;
File = msg.File;
Line = msg.Line;
Routine = msg.Routine;
}
/// <summary>
/// Gets a the PostgreSQL error message and code.
/// </summary>
public override string Message => SqlState + ": " + MessageText;
/// <summary>
/// Specifies whether the exception is considered transient, that is, whether retrying to operation could
/// succeed (e.g. a network error). Check <see cref="SqlState"/>.
/// </summary>
public override bool IsTransient
{
get
{
switch (SqlState)
{
case "53000": //insufficient_resources
case "53100": //disk_full
case "53200": //out_of_memory
case "53300": //too_many_connections
case "53400": //configuration_limit_exceeded
case "57P03": //cannot_connect_now
case "58000": //system_error
case "58030": //io_error
case "40001": //serialization_error
case "55P03": //lock_not_available
case "55006": //object_in_use
case "55000": //object_not_in_prerequisite_state
case "08000": //connection_exception
case "08003": //connection_does_not_exist
case "08006": //connection_failure
case "08001": //sqlclient_unable_to_establish_sqlconnection
case "08004": //sqlserver_rejected_establishment_of_sqlconnection
case "08007": //transaction_resolution_unknown
return true;
default:
return false;
}
}
}
/// <summary>
/// Returns the statement which triggered this exception.
/// </summary>
public NpgsqlStatement Statement { get; internal set; }
/// <summary>
/// Gets a collection of key/value pairs that provide additional PostgreSQL fields about the exception.
/// </summary>
public override IDictionary Data
{
get
{
return _data ?? (_data = (
from p in typeof(PostgresException).GetProperties()
let k = p.Name
where p.Name != nameof(Data)
where p.GetCustomAttribute<PublicAPIAttribute>() != null
let v = p.GetValue(this)
where v != null
where k != nameof(Position) && k != nameof(InternalPosition) || (int)v != 0
select new { Key = k, Value = v }
).ToDictionary(kv => kv.Key, kv => kv.Value)
);
}
}
#region Serialization
#if !NETSTANDARD1_3
PostgresException(SerializationInfo info, StreamingContext context) : base(info, context)
{
Severity = (string)info.GetValue("Severity", typeof(string));
SqlState = (string)info.GetValue("SqlState", typeof(string));
MessageText = (string)info.GetValue("MessageText", typeof(string));
Detail = (string)info.GetValue("Detail", typeof(string));
Hint = (string)info.GetValue("Hint", typeof(string));
Position = (int) info.GetValue("Position", typeof(int));
InternalPosition = (int) info.GetValue("InternalPosition", typeof(int));
InternalQuery = (string)info.GetValue("InternalQuery", typeof(string));
Where = (string)info.GetValue("Where", typeof(string));
SchemaName = (string)info.GetValue("SchemaName", typeof(string));
TableName = (string)info.GetValue("TableName", typeof(string));
ColumnName = (string)info.GetValue("ColumnName", typeof(string));
DataTypeName = (string)info.GetValue("DataTypeName", typeof(string));
ConstraintName = (string)info.GetValue("ConstraintName", typeof(string));
File = (string)info.GetValue("File", typeof(string));
Line = (string)info.GetValue("Line", typeof(string));
Routine = (string)info.GetValue("Routine", typeof(string));
}
/// <summary>
/// Populates a <see cref="SerializationInfo"/> with the data needed to serialize the target object.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> to populate with data.</param>
/// <param name="context">The destination (see <see cref="StreamingContext"/>) for this serialization.</param>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("Severity", Severity);
info.AddValue("SqlState", SqlState);
info.AddValue("MessageText", MessageText);
info.AddValue("Detail", Detail);
info.AddValue("Hint", Hint);
info.AddValue("Position", Position);
info.AddValue("InternalPosition", InternalPosition);
info.AddValue("InternalQuery", InternalQuery);
info.AddValue("Where", Where);
info.AddValue("SchemaName", SchemaName);
info.AddValue("TableName", TableName);
info.AddValue("ColumnName", ColumnName);
info.AddValue("DataTypeName", DataTypeName);
info.AddValue("ConstraintName", ConstraintName);
info.AddValue("File", File);
info.AddValue("Line", Line);
info.AddValue("Routine", Routine);
}
#endif
#endregion
}
}