Err Object related infrastructure is near complete. This is just a
authorJambunathan K <jambunathan@mono-cvs.ximian.com>
Thu, 10 Jun 2004 14:12:47 +0000 (14:12 -0000)
committerJambunathan K <jambunathan@mono-cvs.ximian.com>
Thu, 10 Jun 2004 14:12:47 +0000 (14:12 -0000)
preliminary commit. Requires more thorough review and test cases.

svn path=/trunk/mcs/; revision=29190

mcs/class/Microsoft.VisualBasic/Microsoft.VisualBasic/ChangeLog
mcs/class/Microsoft.VisualBasic/Microsoft.VisualBasic/ErrObject.cs
mcs/class/Microsoft.VisualBasic/Microsoft.VisualBasic/Information.cs
mcs/class/Microsoft.VisualBasic/Microsoft.VisualBasic/Microsoft.VisualBasic.CompilerServices/ChangeLog
mcs/class/Microsoft.VisualBasic/Microsoft.VisualBasic/Microsoft.VisualBasic.CompilerServices/ProjectData.cs
mcs/class/Microsoft.VisualBasic/Microsoft.VisualBasic/VBUtils.cs

index 587fde50a45402ec6951f345ed994db4951be001..51ed9254c981986f713ad9fcef400c36a9b310cb 100644 (file)
@@ -1,3 +1,9 @@
+2004-06-10  Jambunathan K  <kjambunathan@novell.com>
+       * VBUtils.cs: Implemented more GetResourceString() methods
+       * Information.cs: Completed Information.Err
+       * ErrObject.cs: Almost entirely merged from Mainsoft's code
+       All three files need a thorough review.
+
 2004-06-07  Jambunathan  <kjambunathan@novell.com>
        * VariantType.cs: Minor changes
        * VBUtils.txt: Newly created  error string resources for VB.NET
index 54a9c6d1890cc547aae96735c54a795b0f50815a..7e26df07e23e64fa478e14a82cefd4a725e18821 100644 (file)
 //
 
 using System;
+using Microsoft.VisualBasic.CompilerServices;
 
 namespace Microsoft.VisualBasic \r
 {
        sealed public class ErrObject {
-               // Declarations
-               int pHelpContext;
-               int pLastDllError;
-               int pNumber;
-               int pErl;
-               string pSource;
-               string pHelpFile;
-               string pDescription;
-               System.Exception pException;
-               // Constructors
-               // Properties
+
+               private System.Exception pException;
+               private int pErl;
+               private int pNumber;
+               private string pSource;
+               private string pDescription;
+               private string pHelpFile;
+               private int pHelpContext;
+
+               private int pLastDllError;
+
+               private bool m_bDontMapException = false;
+               private bool NumberIsSet = false;
+               private bool ClearOnCapture = false;
+               private bool SourceIsSet = false;
+               private bool DescriptionIsSet = false;
+               private bool HelpFileIsSet = false;
+               private bool HelpContextIsSet = false;
+
+               public ErrObject()
+               {
+                       Clear();
+               }
+
+               public void Clear () 
+               {
+                       pException = null;
+                       pErl = 0;
+                       pNumber = 0;
+                       pSource = "";
+                       pDescription = "";
+                       pHelpFile = "";
+                       pHelpContext = 0;
+
+                       m_bDontMapException = false;
+                       NumberIsSet = false;
+                       ClearOnCapture = false;
+                       SourceIsSet = false;
+                       DescriptionIsSet = false;
+                       HelpFileIsSet = false;
+                       HelpContextIsSet = false;
+                       ClearOnCapture = true;
+               }
+
+               public void CaptureException (Exception ex)
+               {
+                       if(ex == pException)
+                               return;
+                       if(ClearOnCapture == true)
+                               Clear();
+                       else
+                               ClearOnCapture = true;
+
+                       pException = ex;
+               }
+
+               public void CaptureException (Exception ex, int lErl)
+               {
+                       CaptureException(ex);
+                       pErl = lErl;
+               }
+
+               public Exception CreateException (int Number, String Description)
+               {
+                       Exception ex;
+
+                       Clear();
+                       this.Number = Number;
+                       if(Number == 0)
+                               throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
+                       ex = MapNumberToException(pNumber, Description);
+                       this.ClearOnCapture = false;
+                       return ex;
+               }
+
+               private String FilterDefaultMessage(String Msg)
+               {
+                       if(pException == null)
+                               return Msg;
+
+                       string Msg1 = Utils.GetResourceString(this.Number);
+
+                       if(Msg == null || Msg.Length == 0)
+                               return Msg1;
+                       else if(String.CompareOrdinal("Exception from HRESULT: 0x", 0, Msg, 0, System.Math.Min(Msg.Length, 26)) == 0) {
+                               return (Msg1 != null ? Msg1 : Msg);
+                       }
+                       else
+                               return Msg;
+               }
+
+               private Exception MapNumberToException (int Number, String Description)
+               {
+                       bool Ignored = false;
+                       return ExceptionUtils.BuildException(Number, Description, ref Ignored);
+               }
+
+
+               // This function needs to be reviewed
+               private int MapExceptionToNumber (Exception ex)
+               {
+                       int hResult = 0;
+
+                       if(ex is ArgumentException) {
+                               hResult = unchecked((int)0x80070057);
+                       }
+                       else if(ex is ArithmeticException) {
+                               if(ex is NotFiniteNumberException) { 
+                                       if((ex as NotFiniteNumberException).OffendingNumber == 0)
+                                               return 11;
+                                       else
+                                               return 6;
+                               }
+                               else {
+                                       hResult = unchecked((int)0x80070216);     
+                               }
+                       }
+                       else if(ex is ArrayTypeMismatchException) {
+                               hResult = unchecked((int)0x80131503);
+                       }
+                       // else if(exType.Equals(IndexOutOfRangeException)) {
+                       //      hResult = (exType.Equals(IndexOutOfRangeException)).HResult;     
+                       // }
+                       else if(ex is InvalidCastException) {
+                               hResult = unchecked((int)0x80004002);
+                       }
+                       else if(ex is NotSupportedException) {
+                               hResult = unchecked((int)0x80131515);
+                       }
+                       else if(ex is NullReferenceException) {
+                               hResult = unchecked((int)0x80004003);
+                       }
+                       else if(ex is UnauthorizedAccessException) {
+                               hResult = unchecked((int)0x80131500);
+                       }
+
+                       else {
+                               hResult = unchecked((int)0x80004005);
+                       }
+
+                       hResult = ExceptionUtils.getVBFromDotNet(hResult);
+                       if(hResult != 0 )
+                               return hResult;
+                       else
+                               return 5;
+               }
+
+               private void ParseHelpLink (String HelpLink)
+               {
+                       int ind;
+
+                       if(HelpLink == null || HelpLink.Length == 0) {
+                               if(HelpContextIsSet == false)
+                                       this.HelpContext = 0;
+
+                               if (HelpFileIsSet == false)
+                                       this.HelpFile = "";
+                       }
+                       else {
+                               ind = HelpLink.IndexOf("#");
+
+                               if(ind != -1) {
+                                       if(HelpContextIsSet == false) {
+                                               if(ind < HelpLink.Length)
+                                                       this.HelpContext = IntegerType.FromString(HelpLink.Substring(ind + 1));
+                                               else
+                                                       this.HelpContext = 0;
+                                       }
+                                       if (HelpFileIsSet == false)
+                                               this.HelpFile = HelpLink.Substring(0, ind);
+                               }
+                               else {
+                                       if (HelpContextIsSet == false)
+                                               this.HelpContext = 0;
+
+                                       if (!this.HelpFileIsSet)
+                                               this.HelpFile = HelpLink;
+                               }
+                       }
+               }
+
+               public int MapErrorNumber (int Number)
+               {
+                       if(Number > 65535)
+                               throw new ArgumentException(VBUtils.GetResourceString("Argument_InvalidValue1", "Number"));
+
+
+                       if(Number >= 0)
+                               return Number;
+                       else
+                               return ExceptionUtils.fromDotNetToVB(Number);
+               }
+
+               private String MakeHelpLink(String HelpFile, int HelpContext)
+               {
+                       return HelpFile + "#" + StringType.FromInteger(HelpContext);
+               }
+
                [MonoTODO]
-               public System.Int32 HelpContext 
+               public void Raise (System.Int32 Number, 
+                                  [System.Runtime.InteropServices.Optional] 
+                                  [System.ComponentModel.DefaultValue(null)] System.Object Source, 
+                                  [System.Runtime.InteropServices.Optional] 
+                                  [System.ComponentModel.DefaultValue(null)] System.Object Description, 
+                                  [System.Runtime.InteropServices.Optional] 
+                                  [System.ComponentModel.DefaultValue(null)] System.Object HelpFile, 
+                                  [System.Runtime.InteropServices.Optional] 
+                                  [System.ComponentModel.DefaultValue(null)] System.Object HelpContext) 
                { 
-                       get { 
-                               return pHelpContext; 
-                       } 
-                       set { 
-                               pHelpContext = value;
-                       } 
+                       Exception e;
+                       
+                       if(Number == 0)
+                               throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
+
+                       this.Number = Number;
+
+                       if(Source != null)
+                               this.Source = StringType.FromObject(Source);
+                       else
+                               this.Source = "";
+
+                       if(HelpFile != null)
+                               this.HelpFile = StringType.FromObject(HelpFile);
+
+                       if(HelpContext != null)
+                               this.HelpContext = IntegerType.FromObject(HelpContext);
+
+                       if(Description != null)
+                               this.Description = StringType.FromObject(Description);
+                       else if (DescriptionIsSet == false)
+                               this.Description = Utils.GetResourceString(pNumber);
+
+                       e = MapNumberToException(pNumber, pDescription);
+
+
+                       e.Source = pSource;
+                       e.HelpLink = MakeHelpLink(pHelpFile, pHelpContext);
+
+
+                       ClearOnCapture = false;
+                       throw e;
+               } 
+
+               public void SetUnmappedError (int Number)
+               {
+                       Clear();
+                       this.Number = Number;
+                       ClearOnCapture = false;
                }
-               [MonoTODO]
-               public System.Int32 LastDllError {  
-                       get { 
-                               return pLastDllError; 
-                       } 
+
+               public System.Exception GetException () 
+               {
+                       return pException;
                }
-               [MonoTODO]
-               public System.Int32 Number {  
+
+               public System.String Description {  
                        get { 
-                               return pNumber;
+                               if(DescriptionIsSet)
+                                       return pDescription;
+
+                               if(pException == null)
+                                       return "";
+
+                               if(pException is Exception)
+                                       this.Description = FilterDefaultMessage(pException.Message);
+                               else
+                                       this.Description = FilterDefaultMessage(pException.Message);
+                       
+                               return pDescription; 
                        } 
                        set { 
-                               pNumber = value;
+                               pDescription = value;
+                               DescriptionIsSet = true;
                        } 
                }
-               [MonoTODO]
+
                public System.Int32 Erl {  
                        get { 
                                return pErl;
                        } 
                }
-               [MonoTODO]
-               public System.String Source {  
+
+               public System.Int32 HelpContext 
+               { 
                        get { 
-                               return pSource;
-                       } 
+                               if(HelpContextIsSet)
+                                       return pHelpContext; 
+
+                               if(pException != null)
+                               {
+                                       if(pException is Exception)
+                                               ParseHelpLink(pException.HelpLink);
+                                       else
+                                               ParseHelpLink("");
+
+                                       return this.pHelpContext;
+                               }
+                               return 0;
+                       }
+
                        set { 
-                               pSource = value;
+                               pHelpContext = value;
+                               HelpContextIsSet = true;
                        } 
                }
-               [MonoTODO]
+
                public System.String HelpFile {  
                        get { 
-                               return pHelpFile;
+                               if (HelpFileIsSet == true)
+                                       return pHelpFile;
+        
+                               if(pException != null) {
+                                       if(pException is Exception)
+                                               ParseHelpLink((pException as Exception).HelpLink);
+                                       else
+                                               ParseHelpLink("");
+
+                                       return pHelpFile;
+                               }
+                               return "";
                        } 
                        set { 
                                pHelpFile = value;
+                               HelpFileIsSet = true;
                        } 
                }
+
                [MonoTODO]
-               public System.String Description {  
+               public System.Int32 LastDllError {  
                        get { 
-                               return pDescription; 
+                               return 0; 
+                       } 
+               }
+
+               public System.Int32 Number {  
+                       get { 
+                               if(NumberIsSet)
+                                       return pNumber;
+
+                               if(pException == null)
+                                       return 0;
+
+                               this.Number = MapExceptionToNumber(pException);
+                               return pNumber;
                        } 
                        set { 
-                               pDescription = value;
+                               pNumber = MapErrorNumber(value);
+                               NumberIsSet = true;
                        } 
                }
-               // Methods
-               [MonoTODO("We should parse the exception object to obtain VB-like error code. Not a trivial task!")]
-               internal void SetException (Exception ex)
-               {
-                       if (pException != ex) \r
-                       {
-                               pNumber = 0xFFFF;
-                               pSource = ex.Source;
-                               pDescription = ex.Message + "\n" + ex.StackTrace;
-                       }
-               }
 
                [MonoTODO]
-               public System.Exception GetException () 
-               {
-                         return pException;
-               }
-               [MonoTODO]
-               public void Clear () {
-                       pHelpContext=0;
-                       pLastDllError=0;
-                       pNumber=0;
-                       pErl=0;
-                       pSource="";
-                       pHelpFile="";
-                       pDescription="";
-                       pException= null;
-               }
-               [MonoTODO]
-               public void Raise (System.Int32 Number, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(null)] System.Object Source, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(null)] System.Object Description, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(null)] System.Object HelpFile, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(null)] System.Object HelpContext) 
-               { 
-                       throw new NotImplementedException (); 
+               public System.String Source {  
+                       get { 
+                               if(SourceIsSet)
+                                       return pSource;
+
+                               if(pException == null)
+                                       return "";
+
+                               if(pException is Exception)
+                                       this.Source = pException.Source;
+                               else
+                                       this.Source = "";
+
+                               return pSource;
+                       } 
+                       set { 
+                               pSource = value;
+                               SourceIsSet = true;
+                       } 
                }
-               // Events
-       };
-}
\ No newline at end of file
+
+
+       }
+}
+
index 7a49930a5133a72c38a8b9751462b45f764fe42b..ae3e70e119bdda80bec3e6259b6826ea05886490 100644 (file)
@@ -247,17 +247,15 @@ namespace Microsoft.VisualBasic
                        return Array.GetLowerBound(Rank);
                }
 
-               [MonoTODO]
                public static Microsoft.VisualBasic.ErrObject Err() 
                { 
-                       throw new NotImplementedException();
+                       return ProjectData.Err;
                }
 
-               [MonoTODO]
                [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] 
                public static System.Int32 Erl () 
                { 
-                       throw new NotImplementedException(); 
+                       return ProjectData.Err.Erl;
                }
 
                public static System.String TypeName (System.Object VarName) 
index 3492db104007e1a7e6e492102e630e0a41d9252e..7cd829bcbfefc178ff0410a84a00e371d497d46b 100644 (file)
@@ -1,3 +1,6 @@
+2004-06-10  Jambunathan K <kjambunathan@novell.com>
+       * ProjectData.cs: Remerged from Mainsoft's codebase
+
 2004-06-07  Jambunathan K  <kjambunathan@novell.com>
        * VBErrors.cs: Newly adopted from Mainsoft's codebase
        * Utils.cs: Fixed formatting. Also some minor changes.
index 173b1ac912a32578f999dba831421d9f452fb378..08ebd1e2f5e37659c97c99f54eed2e5a0bcd964b 100644 (file)
@@ -16,65 +16,47 @@ using System.Runtime.InteropServices;
 
 namespace Microsoft.VisualBasic.CompilerServices
 {
-       /// <summary>
-       /// FIXME: Summary description for ProjectData.
-       /// </summary>
-       
+
        [MonoTODO]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [StructLayout(LayoutKind.Auto)] 
-       public class ProjectData{
+       public class ProjectData {
 
-               internal static System.Exception projectError;
                internal static int erl;
-               internal static Microsoft.VisualBasic.ErrObject pErr;
+               internal static Microsoft.VisualBasic.ErrObject pErr = new ErrObject();
 
-               internal static Microsoft.VisualBasic.ErrObject Err 
+               internal static System.Exception projectError;
+
+
+               public static void ClearProjectError ()
                {
-                       get
-                       {
-                               if (pErr==null)
-                                       pErr=new ErrObject();
+                       pErr.Clear();
+               }
 
+               internal static Microsoft.VisualBasic.ErrObject Err 
+               {
+                       get {
                                return pErr;
                        }
-                       set
-                       {
-                               pErr = value;
-                       }
                }
 
-               /// <summary>
-               /// FIXME: Summary description for ClearProjectError
-               /// </summary>
-               public static void ClearProjectError()
+               public static Exception CreateProjectError(int hr)
                {
-                       projectError = null;
-                       erl = 0;
+                       pErr.Clear();
+                       return pErr.CreateException(hr, VBUtils.GetResourceString(pErr.MapErrorNumber(hr)));
                }
 
-               /// <summary>
-               /// FIXME: Summary description for SetProjectError
-               /// </summary>
-               /// <param name="ex">FIXME: Required. Summary description for ex</param>
-               [MonoTODO]
-               public static void SetProjectError(System.Exception ex)
+               public static void SetProjectError(Exception ex)
                {
-                       SetProjectError(ex, 0);
+                       pErr.CaptureException(ex);
                }
 
-               /// <summary>
-               /// FIXME: Summary description for SetProjectError
-               /// </summary>
-               /// <param name="ex">FIXME: Required. Summary description for ex</param>
-               /// <param name="lErl">FIXME: Required. Summary description for lErl</param>
-               [MonoTODO]
-               public static void SetProjectError(System.Exception ex, int lErl)
+               public static void SetProjectError(Exception ex, int lErl)
                {
-                       projectError = ex;
-                       erl = lErl;
-                       Err.SetException (ex);
+                       pErr.CaptureException(ex, lErl);
                }
+
                
                /*
                [MonoTODO]
index 2635d558eb2a2e463c253176e30e543b47a98bbc..d9d32bf6e585a89b9a31e1ccac5832d7998bbd60 100755 (executable)
@@ -75,50 +75,39 @@ namespace Microsoft.VisualBasic {
                        return str;
                }
                
-               //              public static string GetResourceString(string key, string paramValue)
-               //              {
-               //                      StringBuilder sb = new StringBuilder(GetResourceString(key));
-               //                      sb.Replace("|1", paramValue);
-               //                      return sb.toString();
-               //              }
+               public static string GetResourceString (string key, string paramValue)
+               {
+                       StringBuilder sb = new StringBuilder(GetResourceString(key));
+                       sb.Replace("|1", paramValue);
+                       return sb.ToString();
+               }
                
-               //              public static string GetResourceString( string key,
-               //                      string paramValue1,
-               //                      string paramValue2)
-               //                      {
-               //                              StringBuilder sb = new StringBuilder(GetResourceString(key));
-               //                              sb.Replace("|1", paramValue1);
-               //                              sb.Replace("|2", paramValue2);
-               //                              return sb.toString();
-               //                      }
+               public static string GetResourceString (string key, string paramValue1, string paramValue2)
+               {
+                       StringBuilder sb = new StringBuilder(GetResourceString(key));
+                       sb.Replace("|1", paramValue1);
+                       sb.Replace("|2", paramValue2);
+                       return sb.ToString();
+               }
                
-               //              public static string GetResourceString(
-               //                      string key,
-               //                      string param1,
-               //                      string param2,
-               //                      string param3)
-               //                      {
-               //                              StringBuilder sb = new StringBuilder(GetResourceString(key));
-               //                              sb.Replace("|1", param1);
-               //                              sb.Replace("|2", param2);
-               //                              sb.Replace("|3", param3);
-               //                              return sb.toString();
-               //                      }
+               public static string GetResourceString (string key, string param1, string param2, string param3)
+               {
+                       StringBuilder sb = new StringBuilder(GetResourceString(key));
+                       sb.Replace("|1", param1);
+                       sb.Replace("|2", param2);
+                       sb.Replace("|3", param3);
+                       return sb.ToString();
+               }
                
-               //              public static string GetResourceString(
-               //                      string key,
-               //                      string param1,
-               //                      string param2,
-               //                      string param3,
-               //                      string param4)
-               //                      {
-               //                              StringBuilder sb = new StringBuilder(GetResourceString(key));
-               //                              sb.Replace("|1", param1);
-               //                              sb.Replace("|2", param2);
-               //                              sb.Replace("|3", param3);
-               //                              sb.Replace("|4", param4);
-               //                              return sb.toString();
-               //                      }
+               public static string GetResourceString (string key, string param1, string param2, string param3, string param4)
+               {
+                       StringBuilder sb = new StringBuilder(GetResourceString(key));
+                       sb.Replace("|1", param1);
+                       sb.Replace("|2", param2);
+                       sb.Replace("|3", param3);
+                       sb.Replace("|4", param4);
+                       return sb.ToString();
+               }
                
                public static string GetResourceString(int ResourceId)
                {
@@ -126,11 +115,11 @@ namespace Microsoft.VisualBasic {
                        return GetResourceString(str);
                }
                
-               //              public static string GetResourceString(int ResourceId, string param1)
-               //                      {
-               //                              string str = "ID" + new Integer(ResourceId).toString();
-               //                              return GetResourceString(str, param1);
-               //                      }
+               public static string GetResourceString(int ResourceId, string param1)
+               {
+                       string str = "ID" + ResourceId.ToString();
+                       return GetResourceString(str, param1);
+               }
                
                //              public static java.lang.Exception VBException(java.lang.Exception ex, int hr)
                //                      {