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.
52 public sealed class NpgsqlError
54 // Logging related values
55 private static readonly String CLASSNAME = "NpgsqlError";
57 private ProtocolVersion protocol_version;
58 private String _severity = "";
59 private String _code = "";
60 private String _message = "";
61 private String _detail = "";
62 private String _hint = "";
63 private String _position = "";
64 private String _where = "";
65 private String _file = "";
66 private String _line = "";
67 private String _routine = "";
70 /// Severity code. All versions.
72 public String Severity
81 /// Error code. PostgreSQL 7.4 and up.
92 /// Terse error message. All versions.
103 /// Detailed error message. PostgreSQL 7.4 and up.
114 /// Suggestion to help resolve the error. PostgreSQL 7.4 and up.
125 /// Position (one based) within the query string where the error was encounterd. PostgreSQL 7.4 and up.
127 public String Position
136 /// Trace back information. PostgreSQL 7.4 and up.
147 /// Source file (in backend) reporting the error. PostgreSQL 7.4 and up.
158 /// Source file line number (in backend) reporting the error. PostgreSQL 7.4 and up.
169 /// Source routine (in backend) reporting the error. PostgreSQL 7.4 and up.
171 public String Routine
180 /// Return a string representation of this error object.
182 public override String ToString()
184 StringBuilder B = new StringBuilder();
186 if (Severity.Length > 0)
188 B.AppendFormat("{0}: ", Severity);
192 B.AppendFormat("{0}: ", Code);
194 B.AppendFormat("{0}", Message);
195 // CHECKME - possibly multi-line, that is yucky
196 // if (Hint.Length > 0) {
197 // B.AppendFormat(" ({0})", Hint);
203 internal NpgsqlError(ProtocolVersion protocolVersion)
205 protocol_version = protocolVersion;
208 internal NpgsqlError(ProtocolVersion protocolVersion, String errorMessage)
210 protocol_version = protocolVersion;
211 _message = errorMessage;
215 /// Backend protocol version in use.
217 internal ProtocolVersion BackendProtocolVersion
221 return protocol_version;
225 internal void ReadFromStream(Stream inputStream, Encoding encoding)
227 switch (protocol_version) {
228 case ProtocolVersion.Version2 :
229 ReadFromStream_Ver_2(inputStream, encoding);
232 case ProtocolVersion.Version3 :
233 ReadFromStream_Ver_3(inputStream, encoding);
239 private void ReadFromStream_Ver_2(Stream inputStream, Encoding encoding)
241 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_2");
246 Raw = PGUtil.ReadString(inputStream, encoding);
248 Parts = Raw.Split(new char[] {':'}, 2);
250 if (Parts.Length == 2)
252 _severity = Parts[0].Trim();
253 _message = Parts[1].Trim();
257 _message = Parts[0].Trim();
261 private void ReadFromStream_Ver_3(Stream inputStream, Encoding encoding)
263 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_3");
265 Int32 messageLength = PGUtil.ReadInt32(inputStream, new Byte[4]);
267 // [TODO] Would this be the right way to do?
268 // Check the messageLength value. If it is 1178686529, this would be the
269 // "FATA" string, which would mean a protocol 2.0 error string.
270 if (messageLength == 1178686529)
275 Raw = "FATA" + PGUtil.ReadString(inputStream, encoding);
277 Parts = Raw.Split(new char[] {':'}, 2);
279 if (Parts.Length == 2)
281 _severity = Parts[0].Trim();
282 _message = Parts[1].Trim();
286 _message = Parts[0].Trim();
289 protocol_version = ProtocolVersion.Version2;
297 field = (Char) inputStream.ReadByte();
299 // Now start to read fields.
302 fieldValue = PGUtil.ReadString(inputStream, encoding);
307 _severity = fieldValue;
313 _message = fieldValue;
316 _detail = fieldValue;
322 _position = fieldValue;
334 _routine = fieldValue;
339 field = (Char) inputStream.ReadByte();