Fixed API: added missing attribute System.Serializable
[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 //
13 // Copyright (c) 2002-2003 Mainsoft Corporation.
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using Microsoft.VisualBasic.CompilerServices;
38 using System.Diagnostics;
39
40 namespace Microsoft.VisualBasic 
41 {
42         sealed public class ErrObject {
43
44                 private System.Exception pException;
45                 private int pErl;
46                 private int pNumber;
47                 private string pSource;
48                 private string pDescription;
49                 private string pHelpFile;
50                 private int pHelpContext;
51
52                 private int pLastDllError;
53
54                 private bool m_bDontMapException = false;
55                 private bool NumberIsSet = false;
56                 private bool ClearOnCapture = false;
57                 private bool SourceIsSet = false;
58                 private bool DescriptionIsSet = false;
59                 private bool HelpFileIsSet = false;
60                 private bool HelpContextIsSet = false;
61
62                 internal ErrObject()
63                 {
64                         Clear();
65                 }
66
67                 public void Clear () 
68                 {
69                         pException = null;
70                         pErl = 0;
71                         pNumber = 0;
72                         pSource = "";
73                         pDescription = "";
74                         pHelpFile = "";
75                         pHelpContext = 0;
76
77                         m_bDontMapException = false;
78                         NumberIsSet = false;
79                         ClearOnCapture = false;
80                         SourceIsSet = false;
81                         DescriptionIsSet = false;
82                         HelpFileIsSet = false;
83                         HelpContextIsSet = false;
84                         ClearOnCapture = true;
85                 }
86
87                 internal void CaptureException (Exception ex)
88                 {
89                         if(ex == pException)
90                                 return;
91                         if(ClearOnCapture == true)
92                                 Clear();
93                         else
94                                 ClearOnCapture = true;
95
96                         pException = ex;
97                 }
98
99                 internal void CaptureException (Exception ex, int lErl)
100                 {
101                         CaptureException(ex);
102                         pErl = lErl;
103                 }
104
105                 internal Exception CreateException (int Number, String Description)
106                 {
107                         Exception ex;
108
109                         Clear();
110                         this.Number = Number;
111                         if(Number == 0)
112                                 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
113                         ex = MapNumberToException(pNumber, Description);
114                         this.ClearOnCapture = false;
115                         return ex;
116                 }
117
118                 private String FilterDefaultMessage(String Msg)
119                 {
120                         if(pException == null)
121                                 return Msg;
122
123                         string Msg1 = Utils.GetResourceString(this.Number);
124
125                         if(Msg == null || Msg.Length == 0)
126                                 return Msg1;
127                         else if(String.CompareOrdinal("Exception from HRESULT: 0x", 0, Msg, 0, System.Math.Min(Msg.Length, 26)) == 0) {
128                                 return (Msg1 != null ? Msg1 : Msg);
129                         }
130                         else
131                                 return Msg;
132                 }
133
134                 private Exception MapNumberToException (int Number, String Description)
135                 {
136                         bool Ignored = false;
137                         return ExceptionUtils.BuildException(Number, Description, ref Ignored);
138                 }
139
140
141                 // This function needs to be reviewed
142                 private int MapExceptionToNumber (Exception ex)
143                 {
144                         int hResult = 0;
145
146                         if(ex is ArgumentException) {
147                                 hResult = unchecked((int)0x80070057);
148                         }
149                         else if(ex is ArithmeticException) {
150                                 if(ex is NotFiniteNumberException) { 
151                                         if((ex as NotFiniteNumberException).OffendingNumber == 0)
152                                                 return 11;
153                                         else
154                                                 return 6;
155                                 }
156                                 else {
157                                         hResult = unchecked((int)0x80070216);     
158                                 }
159                         }
160                         else if(ex is ArrayTypeMismatchException) {
161                                 hResult = unchecked((int)0x80131503);
162                         }
163                         // else if(exType.Equals(IndexOutOfRangeException)) {
164                         //      hResult = (exType.Equals(IndexOutOfRangeException)).HResult;     
165                         // }
166                         else if(ex is InvalidCastException) {
167                                 hResult = unchecked((int)0x80004002);
168                         }
169                         else if(ex is NotSupportedException) {
170                                 hResult = unchecked((int)0x80131515);
171                         }
172                         else if(ex is NullReferenceException) {
173                                 hResult = unchecked((int)0x80004003);
174                         }
175                         else if(ex is UnauthorizedAccessException) {
176                                 hResult = unchecked((int)0x80131500);
177                         }
178
179                         else {
180                                 hResult = unchecked((int)0x80004005);
181                         }
182
183                         hResult = ExceptionUtils.getVBFromDotNet(hResult);
184                         if(hResult != 0 )
185                                 return hResult;
186                         else
187                                 return 5;
188                 }
189
190                 private void ParseHelpLink (String HelpLink)
191                 {
192                         int ind;
193
194                         if(HelpLink == null || HelpLink.Length == 0) {
195                                 if(HelpContextIsSet == false)
196                                         this.HelpContext = 0;
197
198                                 if (HelpFileIsSet == false)
199                                         this.HelpFile = "";
200                         }
201                         else {
202                                 ind = HelpLink.IndexOf("#");
203
204                                 if(ind != -1) {
205                                         if(HelpContextIsSet == false) {
206                                                 if(ind < HelpLink.Length)
207                                                         this.HelpContext = IntegerType.FromString(HelpLink.Substring(ind + 1));
208                                                 else
209                                                         this.HelpContext = 0;
210                                         }
211                                         if (HelpFileIsSet == false)
212                                                 this.HelpFile = HelpLink.Substring(0, ind);
213                                 }
214                                 else {
215                                         if (HelpContextIsSet == false)
216                                                 this.HelpContext = 0;
217
218                                         if (!this.HelpFileIsSet)
219                                                 this.HelpFile = HelpLink;
220                                 }
221                         }
222                 }
223
224                 internal int MapErrorNumber (int Number)
225                 {
226                         if(Number > 65535)
227                                 throw new ArgumentException(VBUtils.GetResourceString("Argument_InvalidValue1", "Number"));
228
229
230                         if(Number >= 0)
231                                 return Number;
232                         else
233                                 return ExceptionUtils.fromDotNetToVB(Number);
234                 }
235
236                 private String MakeHelpLink(String HelpFile, int HelpContext)
237                 {
238                         return HelpFile + "#" + StringType.FromInteger(HelpContext);
239                 }
240
241                 [MonoTODO]
242                 public void Raise (System.Int32 Number, 
243                                    [System.Runtime.InteropServices.Optional] 
244                                    [System.ComponentModel.DefaultValue(null)] System.Object Source, 
245                                    [System.Runtime.InteropServices.Optional] 
246                                    [System.ComponentModel.DefaultValue(null)] System.Object Description, 
247                                    [System.Runtime.InteropServices.Optional] 
248                                    [System.ComponentModel.DefaultValue(null)] System.Object HelpFile, 
249                                    [System.Runtime.InteropServices.Optional] 
250                                    [System.ComponentModel.DefaultValue(null)] System.Object HelpContext) 
251                 { 
252                         Exception e;
253                         
254                         if(Number == 0)
255                                 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
256
257                         this.Number = Number;
258
259                         if(Source != null)
260                                 this.Source = StringType.FromObject(Source);
261                         else
262                                 this.Source = Process.GetCurrentProcess().ProcessName;
263
264                         if(HelpFile != null)
265                                 this.HelpFile = StringType.FromObject(HelpFile);
266
267                         if(HelpContext != null)
268                                 this.HelpContext = IntegerType.FromObject(HelpContext);
269
270                         if(Description != null)
271                                 this.Description = StringType.FromObject(Description);
272                         else if (DescriptionIsSet == false) {
273                                 string desc;
274                                 desc = Utils.GetResourceString(pNumber);
275
276                                 if(desc == null)
277                                         desc = Utils.GetResourceString("ID95");
278
279                                 this.Description = desc;
280                         }
281
282                         e = MapNumberToException(pNumber, pDescription);
283
284
285                         e.Source = pSource;
286                         e.HelpLink = MakeHelpLink(pHelpFile, pHelpContext);
287
288
289                         ClearOnCapture = false;
290                         throw e;
291                 } 
292
293                 internal void SetUnmappedError (int Number)
294                 {
295                         Clear();
296                         this.Number = Number;
297                         ClearOnCapture = false;
298                 }
299
300                 public System.Exception GetException () 
301                 {
302                         return pException;
303                 }
304
305                 public System.String Description {  
306                         get { 
307                                 if(DescriptionIsSet)
308                                         return pDescription;
309
310                                 if(pException == null)
311                                         return "";
312
313                                 this.Description = FilterDefaultMessage(pException.Message);
314                                 return pDescription; 
315                         } 
316                         set { 
317                                 pDescription = value;
318                                 DescriptionIsSet = true;
319                         } 
320                 }
321
322                 public System.Int32 Erl {  
323                         get { 
324                                 return pErl;
325                         } 
326                 }
327
328                 public System.Int32 HelpContext 
329                 { 
330                         get { 
331                                 if(HelpContextIsSet)
332                                         return pHelpContext; 
333
334                                 if(pException != null) {
335                                         ParseHelpLink(pException.HelpLink);
336                                         return this.pHelpContext;
337                                 }
338                                 return 0;
339                         }
340
341                         set { 
342                                 pHelpContext = value;
343                                 HelpContextIsSet = true;
344                         } 
345                 }
346
347                 public System.String HelpFile {  
348                         get { 
349                                 if (HelpFileIsSet == true)
350                                         return pHelpFile;
351         
352                                 if(pException != null) {
353                                         ParseHelpLink((pException as Exception).HelpLink);
354                                         return pHelpFile;
355                                 }
356                                 return "";
357                         } 
358                         set { 
359                                 pHelpFile = value;
360                                 HelpFileIsSet = true;
361                         } 
362                 }
363
364                 [MonoTODO]
365                 public System.Int32 LastDllError {  
366                         get { 
367                                 return 0; 
368                         } 
369                 }
370
371                 public System.Int32 Number {  
372                         get { 
373                                 if(NumberIsSet)
374                                         return pNumber;
375
376                                 if(pException == null)
377                                         return 0;
378
379                                 this.Number = MapExceptionToNumber(pException);
380                                 return pNumber;
381                         } 
382                         set { 
383                                 pNumber = MapErrorNumber(value);
384                                 NumberIsSet = true;
385                         } 
386                 }
387
388                 [MonoTODO]
389                 public System.String Source {  
390                         get { 
391                                 if(SourceIsSet)
392                                         return pSource;
393
394                                 if(pException == null)
395                                         return "";
396
397                                 this.Source = pException.Source;
398                                 return pSource;
399                         } 
400                         set { 
401                                 pSource = value;
402                                 SourceIsSet = true;
403                         } 
404                 }
405
406
407         }
408 }
409