1 // created on 12/7/2003 at 18:36
3 // Npgsql.NpgsqlError.cs
6 // Francisco Jr. (fxjrlists@yahoo.com.br)
8 // Copyright (C) 2002 The Npgsql Development Team
9 // npgsql-general@gborg.postgresql.org
10 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 /// EventArgs class to send Notice parameters, which are just NpgsqlError's in a lighter context.
35 public class NpgsqlNoticeEventArgs : EventArgs
38 /// Notice information.
40 public NpgsqlError Notice = null;
42 internal NpgsqlNoticeEventArgs(NpgsqlError eNotice)
49 /// This class represents the ErrorResponse and NoticeResponse
50 /// message sent from PostgreSQL server.
53 public sealed class NpgsqlError
55 // Logging related values
56 private static readonly String CLASSNAME = "NpgsqlError";
58 private ProtocolVersion protocol_version;
59 private String _severity = String.Empty;
60 private String _code = String.Empty;
61 private String _message = String.Empty;
62 private String _detail = String.Empty;
63 private String _hint = String.Empty;
64 private String _position = String.Empty;
65 private String _where = String.Empty;
66 private String _file = String.Empty;
67 private String _line = String.Empty;
68 private String _routine = String.Empty;
69 private String _errorSql = String.Empty;
72 /// Severity code. All versions.
74 public String Severity
83 /// Error code. PostgreSQL 7.4 and up.
94 /// Terse error message. All versions.
105 /// Detailed error message. PostgreSQL 7.4 and up.
116 /// Suggestion to help resolve the error. PostgreSQL 7.4 and up.
127 /// Position (one based) within the query string where the error was encounterd. PostgreSQL 7.4 and up.
129 public String Position
138 /// Trace back information. PostgreSQL 7.4 and up.
149 /// Source file (in backend) reporting the error. PostgreSQL 7.4 and up.
160 /// Source file line number (in backend) reporting the error. PostgreSQL 7.4 and up.
171 /// Source routine (in backend) reporting the error. PostgreSQL 7.4 and up.
173 public String Routine
182 /// String containing the sql sent which produced this error.
184 public String ErrorSql
196 /// Return a string representation of this error object.
198 public override String ToString()
200 StringBuilder B = new StringBuilder();
202 if (Severity.Length > 0)
204 B.AppendFormat("{0}: ", Severity);
208 B.AppendFormat("{0}: ", Code);
210 B.AppendFormat("{0}", Message);
211 // CHECKME - possibly multi-line, that is yucky
212 // if (Hint.Length > 0) {
213 // B.AppendFormat(" ({0})", Hint);
219 internal NpgsqlError(ProtocolVersion protocolVersion)
221 protocol_version = protocolVersion;
224 internal NpgsqlError(ProtocolVersion protocolVersion, String errorMessage)
226 protocol_version = protocolVersion;
227 _message = errorMessage;
231 /// Backend protocol version in use.
233 internal ProtocolVersion BackendProtocolVersion
237 return protocol_version;
241 internal void ReadFromStream(Stream inputStream, Encoding encoding)
243 switch (protocol_version) {
244 case ProtocolVersion.Version2 :
245 ReadFromStream_Ver_2(inputStream, encoding);
248 case ProtocolVersion.Version3 :
249 ReadFromStream_Ver_3(inputStream, encoding);
255 private void ReadFromStream_Ver_2(Stream inputStream, Encoding encoding)
257 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_2");
262 Raw = PGUtil.ReadString(inputStream, encoding);
264 Parts = Raw.Split(new char[] {':'}, 2);
266 if (Parts.Length == 2)
268 _severity = Parts[0].Trim();
269 _message = Parts[1].Trim();
273 _message = Parts[0].Trim();
277 private void ReadFromStream_Ver_3(Stream inputStream, Encoding encoding)
279 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_3");
281 Int32 messageLength = PGUtil.ReadInt32(inputStream, new Byte[4]);
283 // [TODO] Would this be the right way to do?
284 // Check the messageLength value. If it is 1178686529, this would be the
285 // "FATA" string, which would mean a protocol 2.0 error string.
286 if (messageLength == 1178686529)
291 Raw = "FATA" + PGUtil.ReadString(inputStream, encoding);
293 Parts = Raw.Split(new char[] {':'}, 2);
295 if (Parts.Length == 2)
297 _severity = Parts[0].Trim();
298 _message = Parts[1].Trim();
302 _message = Parts[0].Trim();
305 protocol_version = ProtocolVersion.Version2;
313 field = (Char) inputStream.ReadByte();
315 // Now start to read fields.
318 fieldValue = PGUtil.ReadString(inputStream, encoding);
323 _severity = fieldValue;
329 _message = fieldValue;
332 _detail = fieldValue;
338 _position = fieldValue;
350 _routine = fieldValue;
355 field = (Char) inputStream.ReadByte();