forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNpgsqlException.cs
More file actions
288 lines (249 loc) · 8.88 KB
/
NpgsqlException.cs
File metadata and controls
288 lines (249 loc) · 8.88 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
// created on 12/5/2002 at 23:10
// Npgsql.NpgsqlException.cs
//
// Author:
// Francisco Jr. (fxjrlists@yahoo.com.br)
//
// Copyright (C) 2002 The Npgsql Development Team
// npgsql-general@gborg.postgresql.org
// http://gborg.postgresql.org/project/npgsql/projdisplay.php
//
// 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.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Runtime.Serialization;
namespace Npgsql
{
/// <summary>
/// The exception that is thrown when the PostgreSQL backend reports errors.
/// </summary>
[Serializable]
public sealed class NpgsqlException : DbException
{
private readonly NpgsqlError[] errors;
// Logging related values
//private static readonly String CLASSNAME = MethodBase.GetCurrentMethod().DeclaringType.Name;
private static readonly ResourceManager resman = new ResourceManager(MethodBase.GetCurrentMethod().DeclaringType);
// To allow deserialization.
private NpgsqlException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
IList<NpgsqlError> l = (IList<NpgsqlError>) info.GetValue("errors", typeof (IList<NpgsqlError>));
errors = new NpgsqlError[l.Count];
l.CopyTo(errors, 0);
}
/// <summary>
/// Construct a backend error exception based on a list of one or more
/// backend errors. The basic Exception.Message will be built from the
/// first (usually the only) error in the list.
/// </summary>
internal NpgsqlException(IList<NpgsqlError> errors)
: base(errors[0].ToString())
{
NpgsqlEventLog.LogMsg(resman, "Log_ExceptionOccured", LogLevel.Normal, Message);
this.errors = new NpgsqlError[errors.Count];
errors.CopyTo(this.errors, 0);
}
internal NpgsqlException(String message)
: this(message, null)
{
}
internal NpgsqlException(String message, Exception innerException)
: base(message, innerException)
{
NpgsqlEventLog.LogMsg(resman, "Log_ExceptionOccured", LogLevel.Normal, Message);
errors = new NpgsqlError[] { new NpgsqlError(message) };
}
/// <summary>
/// Get object data.
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
// Add custom data, in this case the list of errors when serializing.
// Thanks Robert Chartier for info: http://www.15seconds.com/issue/020903.htm
//use the info object to add the items you want serialized
info.AddValue("errors", errors, typeof (IList));
}
/// <summary>
/// Provide access to the entire list of errors provided by the PostgreSQL backend.
/// </summary>
public NpgsqlError this[Int32 Index]
{
get { return (NpgsqlError) errors[Index]; }
}
/// <summary>
/// Severity code. All versions.
/// </summary>
public String Severity
{
get { return this[0].Severity; }
}
/// <summary>
/// Error code. PostgreSQL 7.4 and up.
/// </summary>
public String Code
{
get { return this[0].Code; }
}
/// <summary>
/// Basic error message. All versions.
/// </summary>
public String BaseMessage
{
get { return this[0].Message; }
}
/// <summary>
/// Detailed error message. PostgreSQL 7.4 and up.
/// </summary>
public String Detail
{
get { return this[0].Detail; }
}
/// <summary>
/// Suggestion to help resolve the error. PostgreSQL 7.4 and up.
/// </summary>
public String Hint
{
get { return this[0].Hint; }
}
/// <summary>
/// Position (one based) within the query string where the error was encounterd. PostgreSQL 7.4 and up.
/// </summary>
public String Position
{
get { return this[0].Position; }
}
/// <summary>
/// Trace back information. PostgreSQL 7.4 and up.
/// </summary>
public String Where
{
get { return this[0].Where; }
}
/// <summary>
/// Source file (in backend) reporting the error. PostgreSQL 7.4 and up.
/// </summary>
public String File
{
get { return this[0].File; }
}
/// <summary>
/// Source file line number (in backend) reporting the error. PostgreSQL 7.4 and up.
/// </summary>
public String Line
{
get { return this[0].Line; }
}
/// <summary>
/// Source routine (in backend) reporting the error. PostgreSQL 7.4 and up.
/// </summary>
public String Routine
{
get { return this[0].Routine; }
}
/// <summary>
/// Schema name which relates to the error. PostgreSQL 9.3 and up.
/// </summary>
public String SchemaName
{
get { return this[0].SchemaName; }
}
/// <summary>
/// Table name which relates to the error. PostgreSQL 9.3 and up.
/// </summary>
public String TableName
{
get { return this[0].TableName; }
}
/// <summary>
/// Column name which relates to the error. PostgreSQL 9.3 and up.
/// </summary>
public String ColumnName
{
get { return this[0].ColumnName; }
}
/// <summary>
/// Data type of column which relates to the error. PostgreSQL 9.3 and up.
/// </summary>
public String DataTypeName
{
get { return this[0].DataTypeName; }
}
/// <summary>
/// Constraint name which relates to the error. PostgreSQL 9.3 and up.
/// </summary>
public String ConstraintName
{
get { return this[0].ConstraintName; }
}
/// <summary>
/// String containing the sql sent which produced this error.
/// </summary>
public String ErrorSql
{
get { return this[0].ErrorSql; }
}
/// <summary>
/// Returns the entire list of errors provided by the PostgreSQL backend.
/// </summary>
public IList Errors
{
get { return errors; }
}
/// <summary>
/// Format a .NET style exception string.
/// Include all errors in the list, including any hints.
/// </summary>
public override String ToString()
{
if (Errors != null)
{
StringWriter S = new StringWriter();
S.WriteLine("{0}:", this.GetType().FullName);
foreach (NpgsqlError PgError in Errors)
{
AppendString(S, "{0}", PgError.Message);
AppendString(S, "Severity: {0}", PgError.Severity);
AppendString(S, "Code: {0}", PgError.Code);
AppendString(S, "Hint: {0}", PgError.Hint);
}
S.Write(StackTrace);
return S.ToString();
}
return base.ToString();
}
/// <summary>
/// Append a line to the given Stream, first checking for zero-length.
/// </summary>
private static void AppendString(StringWriter Stream, string Format, string Str)
{
if (Str.Length > 0)
{
Stream.WriteLine(Format, Str);
}
}
}
}