1 // created on 12/5/2002 at 23:10
3 // Npgsql.NpgsqlException.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
27 using System
.Resources
;
30 using System
.Collections
;
31 using System
.Runtime
.Serialization
;
36 /// The exception that is thrown when the PostgreSQL backend reports errors.
39 public sealed class NpgsqlException
: ApplicationException
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
) {}
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.
55 internal NpgsqlException(IList errors
) : base(((NpgsqlError
)errors
[0]).ToString())
57 NpgsqlEventLog
.LogMsg(resman
, "Log_ExceptionOccured", LogLevel
.Normal
, Message
);
63 /// Provide access to the entire list of errors provided by the PostgreSQL backend.
65 public NpgsqlError
this[Int32 Index
]
69 return (NpgsqlError
)errors
[Index
];
75 /// Severity code. All versions.
77 public String Severity
81 return this[0].Severity
;
86 /// Error code. PostgreSQL 7.4 and up.
97 /// Basic error message. All versions.
99 public String BaseMessage
103 return this[0].Message
;
108 /// Detailed error message. PostgreSQL 7.4 and up.
114 return this[0].Detail
;
119 /// Suggestion to help resolve the error. PostgreSQL 7.4 and up.
130 /// Position (one based) within the query string where the error was encounterd. PostgreSQL 7.4 and up.
132 public String Position
136 return this[0].Position
;
141 /// Trace back information. PostgreSQL 7.4 and up.
147 return this[0].Where
;
152 /// Source file (in backend) reporting the error. PostgreSQL 7.4 and up.
163 /// Source file line number (in backend) reporting the error. PostgreSQL 7.4 and up.
174 /// Source routine (in backend) reporting the error. PostgreSQL 7.4 and up.
176 public String Routine
180 return this[0].Routine
;
185 /// Returns the entire list of errors provided by the PostgreSQL backend.
196 /// Format a .NET style exception string.
197 /// Include all errors in the list, including any hints.
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
);
220 /// Append a line to the given Stream, first checking for zero-length.
222 private static void AppendString(StringWriter Stream
, string Format
, string Str
)
224 if (Str
.Length
> 0) {
225 Stream
.WriteLine(Format
, Str
);