2 * Firebird ADO.NET Data provider for .NET and Mono
4 * The contents of this file are subject to the Initial
5 * Developer's Public License Version 1.0 (the "License");
6 * you may not use this file except in compliance with the
7 * License. You may obtain a copy of the License at
8 * http://www.firebirdsql.org/index.php?op=doc&id=idpl
10 * Software distributed under the License is distributed on
11 * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
12 * express or implied. See the License for the specific
13 * language governing rights and limitations under the License.
15 * Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16 * All Rights Reserved.
20 using System.Collections;
21 using System.Globalization;
23 using System.Reflection;
24 using System.Resources;
26 namespace FirebirdSql.Data.Common
28 internal sealed class IscException : Exception
32 private IscErrorCollection errors;
33 private int errorCode;
34 private string message;
40 public IscErrorCollection Errors
44 if (this.errors == null)
46 this.errors = new IscErrorCollection();
53 public new string Message
55 get { return this.message; }
60 get { return this.errorCode; }
67 if (this.errors.Count > 0)
69 return this.errors[0].IsWarning;
82 public IscException() : base()
86 public IscException(int errorCode) : base()
88 this.Errors.Add(IscCodes.isc_arg_gds, errorCode);
89 this.BuildExceptionMessage();
92 public IscException(string strParam) : base()
94 this.Errors.Add(IscCodes.isc_arg_string, strParam);
95 this.BuildExceptionMessage();
98 public IscException(int errorCode, int intparam) : base()
100 this.Errors.Add(IscCodes.isc_arg_gds, errorCode);
101 this.Errors.Add(IscCodes.isc_arg_number, intparam);
102 this.BuildExceptionMessage();
105 public IscException(int type, int errorCode, string strParam) : base()
107 this.Errors.Add(type, errorCode);
108 this.Errors.Add(IscCodes.isc_arg_string, strParam);
109 this.BuildExceptionMessage();
112 public IscException(int type, int errorCode, int intParam, string strParam)
115 this.Errors.Add(type, errorCode);
116 this.Errors.Add(IscCodes.isc_arg_string, strParam);
117 this.Errors.Add(IscCodes.isc_arg_number, intParam);
118 this.BuildExceptionMessage();
125 public void BuildExceptionMessage()
127 string resources = "FirebirdSql.Data.Common.Resources.isc_error_msg";
129 StringBuilder builder = new StringBuilder();
130 ResourceManager rm = new ResourceManager(resources, Assembly.GetExecutingAssembly());
132 this.errorCode = (this.Errors.Count != 0) ? this.Errors[0].ErrorCode : 0;
134 for (int i = 0; i < this.Errors.Count; i++)
136 if (this.Errors[i].Type == IscCodes.isc_arg_gds ||
137 this.Errors[i].Type == IscCodes.isc_arg_warning)
139 int code = this.Errors[i].ErrorCode;
140 string message = null;
144 message = rm.GetString(code.ToString());
154 message = String.Format(CultureInfo.CurrentCulture, "No message for error code {0} found.", code);
158 ArrayList param = new ArrayList();
162 while (index < this.Errors.Count && this.Errors[index].IsArgument)
164 param.Add(this.Errors[index++].StrParam);
168 object[] args = (object[])param.ToArray(typeof(object));
172 if (code == IscCodes.isc_except)
174 // Custom exception add the first argument as error code
175 this.errorCode = Convert.ToInt32(args[0], CultureInfo.InvariantCulture);
179 if (builder.Length > 0)
181 builder.Append("\n");
184 builder.AppendFormat(CultureInfo.CurrentCulture, message, args);
189 message = String.Format(CultureInfo.CurrentCulture, "No message for error code {0} found.", code);
191 builder.AppendFormat(CultureInfo.CurrentCulture, message, args);
196 // Update error collection only with the main error
197 IscError mainError = new IscError(this.errorCode);
198 mainError.Message = builder.ToString();
200 this.errors = new IscErrorCollection();
201 this.Errors.Add(mainError);
203 // Update exception message
204 this.message = builder.ToString();