New test.
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / IscException.cs
1 /*
2  *      Firebird ADO.NET Data provider for .NET and Mono 
3  * 
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
9  *
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.
14  * 
15  *      Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16  *      All Rights Reserved.
17  */
18
19 using System;
20 using System.Collections;
21 using System.Globalization;
22 using System.Text;
23 using System.Reflection;
24 using System.Resources;
25
26 namespace FirebirdSql.Data.Common
27 {
28         internal sealed class IscException : Exception
29         {
30                 #region Fields
31
32                 private IscErrorCollection errors;
33                 private int errorCode;
34                 private string message;
35
36                 #endregion
37
38                 #region Properties
39
40                 public IscErrorCollection Errors
41                 {
42                         get
43                         {
44                                 if (this.errors == null)
45                                 {
46                                         this.errors = new IscErrorCollection();
47                                 }
48
49                                 return this.errors;
50                         }
51                 }
52
53                 public new string Message
54                 {
55                         get { return this.message; }
56                 }
57
58                 public int ErrorCode
59                 {
60                         get { return this.errorCode; }
61                 }
62
63                 public bool IsWarning
64                 {
65                         get
66                         {
67                                 if (this.errors.Count > 0)
68                                 {
69                                         return this.errors[0].IsWarning;
70                                 }
71                                 else
72                                 {
73                                         return false;
74                                 }
75                         }
76                 }
77
78                 #endregion
79
80                 #region Constructors
81
82                 public IscException() : base()
83                 {
84                 }
85
86                 public IscException(int errorCode) : base()
87                 {
88                         this.Errors.Add(IscCodes.isc_arg_gds, errorCode);
89                         this.BuildExceptionMessage();
90                 }
91
92                 public IscException(string strParam) : base()
93                 {
94                         this.Errors.Add(IscCodes.isc_arg_string, strParam);
95                         this.BuildExceptionMessage();
96                 }
97
98                 public IscException(int errorCode, int intparam) : base()
99                 {
100                         this.Errors.Add(IscCodes.isc_arg_gds, errorCode);
101                         this.Errors.Add(IscCodes.isc_arg_number, intparam);
102                         this.BuildExceptionMessage();
103                 }
104
105                 public IscException(int type, int errorCode, string strParam) : base()
106                 {
107                         this.Errors.Add(type, errorCode);
108                         this.Errors.Add(IscCodes.isc_arg_string, strParam);
109                         this.BuildExceptionMessage();
110                 }
111
112                 public IscException(int type, int errorCode, int intParam, string strParam) 
113                         : base()
114                 {
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();
119                 }
120
121                 #endregion
122
123                 #region Methods
124
125                 public void BuildExceptionMessage()
126                 {
127                         string resources = "FirebirdSql.Data.Common.Resources.isc_error_msg";                   
128
129                         StringBuilder builder = new StringBuilder();
130                         ResourceManager rm = new ResourceManager(resources, Assembly.GetExecutingAssembly());
131
132                         this.errorCode = (this.Errors.Count != 0) ? this.Errors[0].ErrorCode : 0;
133
134                         for (int i = 0; i < this.Errors.Count; i++)
135                         {
136                                 if (this.Errors[i].Type == IscCodes.isc_arg_gds ||
137                                         this.Errors[i].Type == IscCodes.isc_arg_warning)
138                                 {
139                                         int code = this.Errors[i].ErrorCode;
140                                         string message = null;
141
142                                         try
143                                         {
144                                                 message = rm.GetString(code.ToString());
145                                         }
146                                         catch
147                                         {
148                                                 message = null;
149                                         }
150                                         finally
151                                         {
152                                                 if (message == null)
153                                                 {
154                                                         message = String.Format(CultureInfo.CurrentCulture, "No message for error code {0} found.", code);
155                                                 }
156                                         }
157
158                                         ArrayList param = new ArrayList();
159
160                                         int index = i + 1;
161
162                                         while (index < this.Errors.Count && this.Errors[index].IsArgument)
163                                         {
164                                                 param.Add(this.Errors[index++].StrParam);
165                                                 i++;
166                                         }
167
168                                         object[] args = (object[])param.ToArray(typeof(object));
169
170                                         try
171                                         {
172                                                 if (code == IscCodes.isc_except)
173                                                 {
174                                                         // Custom exception     add     the     first argument as error code
175                                                         this.errorCode = Convert.ToInt32(args[0], CultureInfo.InvariantCulture);
176                                                 }
177                                                 else
178                                                 {
179                                                         if (builder.Length > 0)
180                                                         {
181                                                                 builder.Append("\n");
182                                                         }
183
184                                                         builder.AppendFormat(CultureInfo.CurrentCulture, message, args);
185                                                 }
186                                         }
187                                         catch
188                                         {
189                                                 message = String.Format(CultureInfo.CurrentCulture, "No message for error code {0} found.", code);
190
191                                                 builder.AppendFormat(CultureInfo.CurrentCulture, message, args);
192                                         }
193                                 }
194                         }
195
196                         // Update error collection only with the main error
197                         IscError mainError = new IscError(this.errorCode);
198                         mainError.Message = builder.ToString();
199
200                         this.errors = new IscErrorCollection();
201                         this.Errors.Add(mainError);
202
203                         // Update exception     message
204                         this.message = builder.ToString();
205                 }
206
207                 #endregion
208         }
209 }