We decided to use the exception handling library to solve all these problems.
-
Replace Policy : Should be used for business exceptions
-
Wrap Policy : For any un-handled errors
-
Propagate Policy : In all classes / functions other than the entry/exit points
Existing replace Policy functionality
-
Currently the replace policy is configured as below, a replace message to each of the type of the exception. Here the configuration is to handle "System.Security.SecurityException"
<add name="Replace Policy">
<exceptionTypes>
<add name="SecurityException" type="System.Security.SecurityException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException">
<exceptionHandlers>
<add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" exceptionMessage="Replaced Exception: User is not authorized to peform the requested action." replaceExceptionType="System.ApplicationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
-
To call this you need to raise a "System.Security.SecurityException" and the error message will be replaced by this message.
You can add more exception types like database under the "Replace Policy", with different messages.
Wrap Policy Functionality
<add name="Wrap Policy">
<exceptionTypes>
<add name="NonHandledErrors" type="System.Exception" postHandlingAction="ThrowNewException">
<exceptionHandlers>
<add name="Wrap Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" exceptionMessage="A recoverable error occurred while processing your request, please try after some time" wrapExceptionType="System.Exception" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
Our requirement
In our case the exception type will be "BusinessException" (Custom exception) in case of any business related exceptions and and system.Exception for all other un-handled exception.
Here we are defined two error codes and its messages, 100001, 100002.
<add name="Replace Policy">
<exceptionTypes>
<add name="BusinessLayerException" type="System.Exception" postHandlingAction="ThrowNewException">
<exceptionHandlers>
<add name="100001" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" exceptionMessage="No Flight options found for this query" replaceExceptionType="System.ApplicationException" />
<add name="100002" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" exceptionMessage="Not able to book your requrierment" replaceExceptionType="System.ApplicationException" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
We found that, the Enterprise Library exception replace handler required some modification and will be able to cope this requirements. It's good to see that Microsoft shared the source code of the exception handling, and thus, modified some classes to meet our requirements.
Code for the entry/exist point in your code
You need to handle the error here
catch (Exception ex)
{
bool rethrow = false;
if (ex.GetType().FullName.Equals("ExceptionHandling.BusinessException"))
{
rethrow = ExceptionPolicy.HandleException(ex, EnumExceptionPolicyTypes.Type.Replace_Policy);
}
else
{
rethrow = ExceptionPolicy.HandleException(ex, EnumExceptionPolicyTypes.Type.Wrap_Policy);
}
if (rethrow)
{
throw;
}
}
Code for all other codes / functions
catch (Exception ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex, EnumExceptionPolicyTypes.Type.Propagate_Policy);
if (rethrow)
{
throw;
}
}
Way to rise raises your own exception
throw new BusinessException("100001");
Customer business exception Class
public class BusinessException : System.ApplicationException
{
public BusinessException() : base() { }
public BusinessException(string message) : base(message) { }
public BusinessException(string message, System.Exception inner) : base(message, inner) {
}