views:

391

answers:

2

Hi, all. I have the following region I keep snippetting after the try {} blocks. It's purpose is to provide debugging information whenever configured back to the database ... It does debug nicely ( not all it is supposed to debug , but enough to find exactly the error, BUT ..

The code smells. Even I can get it. Do you have a better idea of error handling for db methods ( I am working currently on log4net implementation on it )

Here is the code:

region CatchExceptionsAdv5NoReturnAtAll

 catch (NullReferenceException nre)
 {
  if (userObj == null)
  {
   userObj = new FB.User ();
   FB.User.GiveDefaultUser ( ref userObj );
  }

  System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
  string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
  string className = st.GetFrame ( 1 ).GetFileName ();
  int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

  string encryptedErrorCode = String.Empty;
  encryptedErrorCode += "className - " + className + " methodName ";
  encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
  encryptedErrorCode += userObj.DomainName;

  if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
   encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


  userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
  userObj.Mc.ClassName += className + " ; ";
  userObj.Mc.MethodName += methodName + " ; ";
  userObj.Mc.DebugMsg += nre.Message;
  //
  if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
  {
   userObj.Mc.Msg = "Failed to debug application error at " + methodName;
   //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
  }

 } //eof catch

 catch (System.InvalidOperationException ioe) //comm -- occurs when no result set was found !!!
 {
  if (userObj == null)
  {
   userObj = new FB.User ();
   FB.User.GiveDefaultUser ( ref userObj );
  }

  System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
  string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
  string className = st.GetFrame ( 1 ).GetFileName ();
  int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

  string encryptedErrorCode = String.Empty;
  encryptedErrorCode += "className - " + className + " methodName ";
  encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
  encryptedErrorCode += userObj.DomainName;

  if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
   encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


  userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
  userObj.Mc.ClassName += className + " ; ";
  userObj.Mc.MethodName += methodName + " ; ";
  userObj.Mc.DebugMsg += ioe.Message;
  //
  if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
  {
   userObj.Mc.Msg = "Failed to debug application error at " + methodName;
   //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
  }

 } //eof catch (System.InvalidOperationException)

 catch (System.IndexOutOfRangeException ioore) //comm -- occurs when no result set was found !!!
 {
  if (userObj == null)
  {
   userObj = new FB.User ();
   FB.User.GiveDefaultUser ( ref userObj );
  }

  System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
  string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
  string className = st.GetFrame ( 1 ).GetFileName ();
  int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

  string encryptedErrorCode = String.Empty;
  encryptedErrorCode += "className - " + className + " methodName ";
  encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
  encryptedErrorCode += userObj.DomainName;

  if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
   encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


  userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
  userObj.Mc.ClassName += className + " ; ";
  userObj.Mc.MethodName += methodName + " ; ";
  userObj.Mc.DebugMsg += ioore.Message;
  //
  if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
  {
   userObj.Mc.Msg = "Failed to debug application error at " + methodName;
   //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
  }

 } //eof catch (System.IndexOutOfRangeException)

 catch (System.Data.SqlClient.SqlException sqle)
 {
  if (userObj == null)
  {
   userObj = new FB.User ();
   FB.User.GiveDefaultUser ( ref userObj );
  }

  System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
  string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
  string className = st.GetFrame ( 1 ).GetFileName ();
  int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

  string encryptedErrorCode = String.Empty;
  encryptedErrorCode += "className - " + className + " methodName ";
  encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
  encryptedErrorCode += userObj.DomainName;

  if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
   encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


  userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
  userObj.Mc.ClassName += className + " ; ";
  userObj.Mc.MethodName += methodName + " ; ";
  userObj.Mc.DebugMsg += sqle.Message;
  //
  if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
  {
   userObj.Mc.Msg = "Failed to debug application error at " + methodName;
   //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
  }

 } //eof catch

 catch (System.FormatException fe)
 {
  if (userObj == null)
  {
   userObj = new FB.User ();
   FB.User.GiveDefaultUser ( ref userObj );
  }

  System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
  string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
  string className = st.GetFrame ( 1 ).GetFileName ();
  int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

  string encryptedErrorCode = String.Empty;
  encryptedErrorCode += "className - " + className + " methodName ";
  encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
  encryptedErrorCode += userObj.DomainName;

  if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
   encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


  userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
  userObj.Mc.ClassName += className + " ; ";
  userObj.Mc.MethodName += methodName + " ; ";
  userObj.Mc.DebugMsg += fe.Message;
  //
  if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
  {
   userObj.Mc.Msg = "Failed to debug application error at " + methodName;
   //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
  }

 } //eof catch

 catch (Exception ex)
 {
  if (userObj == null)
  {
   userObj = new FB.User ();
   FB.User.GiveDefaultUser ( ref userObj );
  }

  System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
  string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
  string className = st.GetFrame ( 1 ).GetFileName ();
  int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

  string encryptedErrorCode = String.Empty;
  encryptedErrorCode += "className - " + className + " methodName ";
  encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
  encryptedErrorCode += userObj.DomainName;

  if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
   encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


  userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
  userObj.Mc.ClassName += className + " ; ";
  userObj.Mc.MethodName += methodName + " ; ";
  userObj.Mc.DebugMsg += ex.Message;
  //
  if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
  {
   userObj.Mc.Msg = "Failed to debug application error at " + methodName;
   //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
  }

 } //eof catch
 #endregion CatchExceptionsAdv5NoReturnAtAll
+2  A: 

You should take a look at the exception handling block in Enterprise Library. It will let you do a lot of what you want to do without repeating such large code blocks. But, in general, why not factor out all the repeated code you have there for each exception. At a minimum, if you don't want to use EntLib, factor out all the repeated code and build yourself a library of methods you can cleanly call from exception handling blocks to do what you want to do.

JP Alioto
Something like this ?!catch ( ExceptionType et ) {StackTrace st = new StackTrace ();CallToExceptionHanlingMethod ( st , userObj , et ) ;return false ;}
YordanGeorgiev
Yes, that will do it. It will clean up your code substantially and give you a centralized location to handle your exceptions and do logging.
JP Alioto
A: 

I quess the real answer would be either using Enterprise Library or log4net ... Meanwhile the quick and dirty way of refactoring was to replace the region with the following call:

  catch (Exception ex)
  {
    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
    Utils.ErrorHandler.Trap ( ref userObj, st, ex );
    return false;
  } //eof catch

which pointed to the Error Handler class presented bellow, which does not improve the error reporting neither provides better architecture for error reporting ... so far it just removed the duplicated code and improved readability:

using System;

namespace Utils
{
  public class ErrorHandler
  {


    public static void Trap ( ref FB.User userObj, System.Diagnostics.StackTrace st, Exception ex )
    {
      if (userObj == null)
      {
        userObj = new FB.User ();
        FB.User.GiveDefaultUser ( ref userObj );
      }


      if (ex is NullReferenceException)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.InvalidOperationException) //comm -- occurs when no result set was found !!!
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.IndexOutOfRangeException) //comm -- occurs when no result set was found !!!
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.Data.SqlClient.SqlException)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.FormatException)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is Exception)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof catch


    } //eof method 



  }//eof class CustomException
} //eof namespace U
YordanGeorgiev