2

In my application when exception occurs i need to pass the Error Code from the Business Layer to the Presentation layer here based on the error code i need to show the message that is available in the DB.
I want to know how to pass the error code from BL and get the Error code in the Presentation Layer.
For logging Exceptions i am using log4net and Enterprise library 4.0.

Thanks in advance

ravithejag
  • 588
  • 11
  • 23
  • http://stackoverflow.com/questions/7135492/asp-net-displaying-business-layer-errors-in-the-presentation-layer – Gopesh Sharma Apr 26 '13 at 07:07
  • http://stackoverflow.com/questions/2210841/what-is-the-right-way-to-pass-on-an-exception-c – Freelancer Apr 26 '13 at 07:08
  • 1
    I suggest you to write yourself a class Log with public static method doLogToMyDatabase(String aError); then when you catch an exeption just call that method that will write to your database what you need. – Don Angelo Annoni Apr 26 '13 at 07:12
  • @DonAngeloAnnoni ..or better yet, an interface, then implement that interface using some class. Done right, that will make it possible to replace that class with pretty much _anything_ later if you need something changed. – Kjartan Apr 26 '13 at 07:32

1 Answers1

1

You can create your own Business Exception inheriting from Exception, and make the class accept your error code. This class will be part of your domain, since it's a business exception. Nothing to do with infrastructure exceptions like DB exceptions..

public class BusinessException : Exception
{
  public int ErrorCode {get; private set;}

  public BusinessException(int errorCode)
  {
     ErrorCode = errorCode;
  }
}

You can also use enums, or constants. I don't know your ErrorCode type.

In your business layer, you can throw the exception by:

throw new BusinessException(10);  //If you are using int
throw new BusinessException(ErrorCodes.Invalid); //If you are using Enums
throw new BusinessException("ERROR_INVALID"); //

So after in your presentation layer you can catch this exception and process it as you need.

public void PresentationMethod()
{
   try
   {
      _bll.BusinessMethod();
   }
   catch(BusinessException be)
   {
      var errorMessage = GetErrorMessage(be.ErrorCode);
      ShowErrorUI(errorMessage);
   }
}
margabit
  • 2,924
  • 18
  • 24