2004-06-24 Anirban Bhattacharjee <banirban@novell.com>
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / ErrObject.cs
1 //
2 // ErrObject.cs
3 //
4 // Author:
5 //   Chris J Breisch (cjbreisch@altavista.net) 
6 //   Francesco Delfino (pluto@tipic.com)
7 //
8 // (C) 2002 Chris J Breisch
9 //     2002 pluto@tipic.com
10 //
11
12 using System;
13 using Microsoft.VisualBasic.CompilerServices;
14 using System.Diagnostics;
15
16 namespace Microsoft.VisualBasic 
17 {
18         sealed public class ErrObject {
19
20                 private System.Exception pException;
21                 private int pErl;
22                 private int pNumber;
23                 private string pSource;
24                 private string pDescription;
25                 private string pHelpFile;
26                 private int pHelpContext;
27
28                 private int pLastDllError;
29
30                 private bool m_bDontMapException = false;
31                 private bool NumberIsSet = false;
32                 private bool ClearOnCapture = false;
33                 private bool SourceIsSet = false;
34                 private bool DescriptionIsSet = false;
35                 private bool HelpFileIsSet = false;
36                 private bool HelpContextIsSet = false;
37
38                 internal ErrObject()
39                 {
40                         Clear();
41                 }
42
43                 public void Clear () 
44                 {
45                         pException = null;
46                         pErl = 0;
47                         pNumber = 0;
48                         pSource = "";
49                         pDescription = "";
50                         pHelpFile = "";
51                         pHelpContext = 0;
52
53                         m_bDontMapException = false;
54                         NumberIsSet = false;
55                         ClearOnCapture = false;
56                         SourceIsSet = false;
57                         DescriptionIsSet = false;
58                         HelpFileIsSet = false;
59                         HelpContextIsSet = false;
60                         ClearOnCapture = true;
61                 }
62
63                 internal void CaptureException (Exception ex)
64                 {
65                         if(ex == pException)
66                                 return;
67                         if(ClearOnCapture == true)
68                                 Clear();
69                         else
70                                 ClearOnCapture = true;
71
72                         pException = ex;
73                 }
74
75                 internal void CaptureException (Exception ex, int lErl)
76                 {
77                         CaptureException(ex);
78                         pErl = lErl;
79                 }
80
81                 internal Exception CreateException (int Number, String Description)
82                 {
83                         Exception ex;
84
85                         Clear();
86                         this.Number = Number;
87                         if(Number == 0)
88                                 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
89                         ex = MapNumberToException(pNumber, Description);
90                         this.ClearOnCapture = false;
91                         return ex;
92                 }
93
94                 private String FilterDefaultMessage(String Msg)
95                 {
96                         if(pException == null)
97                                 return Msg;
98
99                         string Msg1 = Utils.GetResourceString(this.Number);
100
101                         if(Msg == null || Msg.Length == 0)
102                                 return Msg1;
103                         else if(String.CompareOrdinal("Exception from HRESULT: 0x", 0, Msg, 0, System.Math.Min(Msg.Length, 26)) == 0) {
104                                 return (Msg1 != null ? Msg1 : Msg);
105                         }
106                         else
107                                 return Msg;
108                 }
109
110                 private Exception MapNumberToException (int Number, String Description)
111                 {
112                         bool Ignored = false;
113                         return ExceptionUtils.BuildException(Number, Description, ref Ignored);
114                 }
115
116
117                 // This function needs to be reviewed
118                 private int MapExceptionToNumber (Exception ex)
119                 {
120                         int hResult = 0;
121
122                         if(ex is ArgumentException) {
123                                 hResult = unchecked((int)0x80070057);
124                         }
125                         else if(ex is ArithmeticException) {
126                                 if(ex is NotFiniteNumberException) { 
127                                         if((ex as NotFiniteNumberException).OffendingNumber == 0)
128                                                 return 11;
129                                         else
130                                                 return 6;
131                                 }
132                                 else {
133                                         hResult = unchecked((int)0x80070216);     
134                                 }
135                         }
136                         else if(ex is ArrayTypeMismatchException) {
137                                 hResult = unchecked((int)0x80131503);
138                         }
139                         // else if(exType.Equals(IndexOutOfRangeException)) {
140                         //      hResult = (exType.Equals(IndexOutOfRangeException)).HResult;     
141                         // }
142                         else if(ex is InvalidCastException) {
143                                 hResult = unchecked((int)0x80004002);
144                         }
145                         else if(ex is NotSupportedException) {
146                                 hResult = unchecked((int)0x80131515);
147                         }
148                         else if(ex is NullReferenceException) {
149                                 hResult = unchecked((int)0x80004003);
150                         }
151                         else if(ex is UnauthorizedAccessException) {
152                                 hResult = unchecked((int)0x80131500);
153                         }
154
155                         else {
156                                 hResult = unchecked((int)0x80004005);
157                         }
158
159                         hResult = ExceptionUtils.getVBFromDotNet(hResult);
160                         if(hResult != 0 )
161                                 return hResult;
162                         else
163                                 return 5;
164                 }
165
166                 private void ParseHelpLink (String HelpLink)
167                 {
168                         int ind;
169
170                         if(HelpLink == null || HelpLink.Length == 0) {
171                                 if(HelpContextIsSet == false)
172                                         this.HelpContext = 0;
173
174                                 if (HelpFileIsSet == false)
175                                         this.HelpFile = "";
176                         }
177                         else {
178                                 ind = HelpLink.IndexOf("#");
179
180                                 if(ind != -1) {
181                                         if(HelpContextIsSet == false) {
182                                                 if(ind < HelpLink.Length)
183                                                         this.HelpContext = IntegerType.FromString(HelpLink.Substring(ind + 1));
184                                                 else
185                                                         this.HelpContext = 0;
186                                         }
187                                         if (HelpFileIsSet == false)
188                                                 this.HelpFile = HelpLink.Substring(0, ind);
189                                 }
190                                 else {
191                                         if (HelpContextIsSet == false)
192                                                 this.HelpContext = 0;
193
194                                         if (!this.HelpFileIsSet)
195                                                 this.HelpFile = HelpLink;
196                                 }
197                         }
198                 }
199
200                 internal int MapErrorNumber (int Number)
201                 {
202                         if(Number > 65535)
203                                 throw new ArgumentException(VBUtils.GetResourceString("Argument_InvalidValue1", "Number"));
204
205
206                         if(Number >= 0)
207                                 return Number;
208                         else
209                                 return ExceptionUtils.fromDotNetToVB(Number);
210                 }
211
212                 private String MakeHelpLink(String HelpFile, int HelpContext)
213                 {
214                         return HelpFile + "#" + StringType.FromInteger(HelpContext);
215                 }
216
217                 [MonoTODO]
218                 public void Raise (System.Int32 Number, 
219                                    [System.Runtime.InteropServices.Optional] 
220                                    [System.ComponentModel.DefaultValue(null)] System.Object Source, 
221                                    [System.Runtime.InteropServices.Optional] 
222                                    [System.ComponentModel.DefaultValue(null)] System.Object Description, 
223                                    [System.Runtime.InteropServices.Optional] 
224                                    [System.ComponentModel.DefaultValue(null)] System.Object HelpFile, 
225                                    [System.Runtime.InteropServices.Optional] 
226                                    [System.ComponentModel.DefaultValue(null)] System.Object HelpContext) 
227                 { 
228                         Exception e;
229                         
230                         if(Number == 0)
231                                 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
232
233                         this.Number = Number;
234
235                         if(Source != null)
236                                 this.Source = StringType.FromObject(Source);
237                         else
238                                 this.Source = Process.GetCurrentProcess().ProcessName;
239
240                         if(HelpFile != null)
241                                 this.HelpFile = StringType.FromObject(HelpFile);
242
243                         if(HelpContext != null)
244                                 this.HelpContext = IntegerType.FromObject(HelpContext);
245
246                         if(Description != null)
247                                 this.Description = StringType.FromObject(Description);
248                         else if (DescriptionIsSet == false) {
249                                 string desc;
250                                 desc = Utils.GetResourceString(pNumber);
251
252                                 if(desc == null)
253                                         desc = Utils.GetResourceString("ID95");
254
255                                 this.Description = desc;
256                         }
257
258                         e = MapNumberToException(pNumber, pDescription);
259
260
261                         e.Source = pSource;
262                         e.HelpLink = MakeHelpLink(pHelpFile, pHelpContext);
263
264
265                         ClearOnCapture = false;
266                         throw e;
267                 } 
268
269                 internal void SetUnmappedError (int Number)
270                 {
271                         Clear();
272                         this.Number = Number;
273                         ClearOnCapture = false;
274                 }
275
276                 public System.Exception GetException () 
277                 {
278                         return pException;
279                 }
280
281                 public System.String Description {  
282                         get { 
283                                 if(DescriptionIsSet)
284                                         return pDescription;
285
286                                 if(pException == null)
287                                         return "";
288
289                                 this.Description = FilterDefaultMessage(pException.Message);
290                                 return pDescription; 
291                         } 
292                         set { 
293                                 pDescription = value;
294                                 DescriptionIsSet = true;
295                         } 
296                 }
297
298                 public System.Int32 Erl {  
299                         get { 
300                                 return pErl;
301                         } 
302                 }
303
304                 public System.Int32 HelpContext 
305                 { 
306                         get { 
307                                 if(HelpContextIsSet)
308                                         return pHelpContext; 
309
310                                 if(pException != null) {
311                                         ParseHelpLink(pException.HelpLink);
312                                         return this.pHelpContext;
313                                 }
314                                 return 0;
315                         }
316
317                         set { 
318                                 pHelpContext = value;
319                                 HelpContextIsSet = true;
320                         } 
321                 }
322
323                 public System.String HelpFile {  
324                         get { 
325                                 if (HelpFileIsSet == true)
326                                         return pHelpFile;
327         
328                                 if(pException != null) {
329                                         ParseHelpLink((pException as Exception).HelpLink);
330                                         return pHelpFile;
331                                 }
332                                 return "";
333                         } 
334                         set { 
335                                 pHelpFile = value;
336                                 HelpFileIsSet = true;
337                         } 
338                 }
339
340                 [MonoTODO]
341                 public System.Int32 LastDllError {  
342                         get { 
343                                 return 0; 
344                         } 
345                 }
346
347                 public System.Int32 Number {  
348                         get { 
349                                 if(NumberIsSet)
350                                         return pNumber;
351
352                                 if(pException == null)
353                                         return 0;
354
355                                 this.Number = MapExceptionToNumber(pException);
356                                 return pNumber;
357                         } 
358                         set { 
359                                 pNumber = MapErrorNumber(value);
360                                 NumberIsSet = true;
361                         } 
362                 }
363
364                 [MonoTODO]
365                 public System.String Source {  
366                         get { 
367                                 if(SourceIsSet)
368                                         return pSource;
369
370                                 if(pException == null)
371                                         return "";
372
373                                 this.Source = pException.Source;
374                                 return pSource;
375                         } 
376                         set { 
377                                 pSource = value;
378                                 SourceIsSet = true;
379                         } 
380                 }
381
382
383         }
384 }
385