Replacing an exception is one of the common requirements to avoid exposing more than the required information especially while dealing with sensitive information. Imagine a scenario where a component throws the original exception, which might contain sensitive information such as connection string, stack trace, and so on, to the consumer. The Exception Handling Application Block provides the Replace handler to replace the exception with a custom exception and message.
Let us update the existing configuration file with a Replace handler. In this scenario, we will replace a DbException
with an ApplicationException
and set a custom message. This prevents the Data Access Layer from exposing sensitive information such as connection string and so on.
So far we have successfully configured the application with a policy that will process an exception of type DbException
or any matching exception in the class hierarchy. It also replaces the exception with a new System.ApplicationException
with the specified exception message; a new exception is thrown as configured in the post-handling action.
The following is the execution result of Exception Replace Demo provided as part of the sample application with this book:
Exception Message: =================== Replaced Exception: Application exception occured. Error code: 6a45a1e8-e131-421d-a1f4-7a73cdb16198
As we can see in the above result, the original DbException
has been replaced with ApplicationException
. Additionally, the message also provides a unique error code to trace the root of the exception. This helps in protecting sensitive data from being exposed to other layers or users.
13.59.209.131