**** Merged from MCS ****
[mono-project.git] / mcs / class / Npgsql / Npgsql / NpgsqlException.cs
blobe3c2274f1ae97acb5527ba38058ab43cdcb813b6
1 // created on 12/5/2002 at 23:10
3 // Npgsql.NpgsqlException.cs
4 //
5 // Author:
6 // Francisco Jr. (fxjrlists@yahoo.com.br)
7 //
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
26 using System;
27 using System.Resources;
28 using System.IO;
29 using System.Text;
30 using System.Collections;
31 using System.Runtime.Serialization;
33 namespace Npgsql
35 /// <summary>
36 /// The exception that is thrown when the PostgreSQL backend reports errors.
37 /// </summary>
38 [Serializable]
39 public sealed class NpgsqlException : ApplicationException
41 private IList errors;
43 // Logging related values
44 private static readonly String CLASSNAME = "NpgsqlException";
45 private static ResourceManager resman = new ResourceManager(typeof(NpgsqlException));
47 // To allow deserialization.
48 private NpgsqlException(SerializationInfo info, StreamingContext context) : base(info, context) {}
50 /// <summary>
51 /// Construct a backend error exception based on a list of one or more
52 /// backend errors. The basic Exception.Message will be built from the
53 /// first (usually the only) error in the list.
54 /// </summary>
55 internal NpgsqlException(IList errors) : base(((NpgsqlError)errors[0]).ToString())
57 NpgsqlEventLog.LogMsg(resman, "Log_ExceptionOccured", LogLevel.Normal, Message);
58 this.errors = errors;
62 /// <summary>
63 /// Provide access to the entire list of errors provided by the PostgreSQL backend.
64 /// </summary>
65 public NpgsqlError this[Int32 Index]
67 get
69 return (NpgsqlError)errors[Index];
74 /// <summary>
75 /// Severity code. All versions.
76 /// </summary>
77 public String Severity
79 get
81 return this[0].Severity;
85 /// <summary>
86 /// Error code. PostgreSQL 7.4 and up.
87 /// </summary>
88 public String Code
90 get
92 return this[0].Code;
96 /// <summary>
97 /// Basic error message. All versions.
98 /// </summary>
99 public String BaseMessage
103 return this[0].Message;
107 /// <summary>
108 /// Detailed error message. PostgreSQL 7.4 and up.
109 /// </summary>
110 public String Detail
114 return this[0].Detail;
118 /// <summary>
119 /// Suggestion to help resolve the error. PostgreSQL 7.4 and up.
120 /// </summary>
121 public String Hint
125 return this[0].Hint;
129 /// <summary>
130 /// Position (one based) within the query string where the error was encounterd. PostgreSQL 7.4 and up.
131 /// </summary>
132 public String Position
136 return this[0].Position;
140 /// <summary>
141 /// Trace back information. PostgreSQL 7.4 and up.
142 /// </summary>
143 public String Where
147 return this[0].Where;
151 /// <summary>
152 /// Source file (in backend) reporting the error. PostgreSQL 7.4 and up.
153 /// </summary>
154 public String File
158 return this[0].File;
162 /// <summary>
163 /// Source file line number (in backend) reporting the error. PostgreSQL 7.4 and up.
164 /// </summary>
165 public String Line
169 return this[0].Line;
173 /// <summary>
174 /// Source routine (in backend) reporting the error. PostgreSQL 7.4 and up.
175 /// </summary>
176 public String Routine
180 return this[0].Routine;
184 /// <summary>
185 /// Returns the entire list of errors provided by the PostgreSQL backend.
186 /// </summary>
187 public IList Errors
191 return errors;
195 /// <summary>
196 /// Format a .NET style exception string.
197 /// Include all errors in the list, including any hints.
198 /// </summary>
199 public override String ToString()
201 StringWriter S = new StringWriter();
203 S.WriteLine("{0}:", this.GetType().FullName);
205 foreach (NpgsqlError PgError in Errors)
207 AppendString(S, "{0}", PgError.Message);
208 AppendString(S, "Severity: {0}", PgError.Severity);
209 AppendString(S, "Code: {0}", PgError.Code);
210 AppendString(S, "Hint: {0}", PgError.Hint);
213 S.Write(StackTrace);
215 return S.ToString();
219 /// <summary>
220 /// Append a line to the given Stream, first checking for zero-length.
221 /// </summary>
222 private static void AppendString(StringWriter Stream, string Format, string Str)
224 if (Str.Length > 0) {
225 Stream.WriteLine(Format, Str);