title: Wrap an exception in an Interceptor with async invocation target
body: <p>Is it possible to write a Castle DynamicProxy Interceptor (for use with an IoC framework) that will allow you to wrap an exception being thrown by the class being intercepted?</p>
  
  <p>I have a class which is throwing a <code>SqlException</code>, and I want to have that wrapped in a custom class called <code>MyCustomSqlException</code>.  When the intercepted class is synchronous then this is trivial because I can use a simple try catch block to deal with wrapping the <code>SqlException</code> and throwing the <code>MyCustomSqlException</code>.</p>
  
  <p>The problem comes in when the intercepted class has async methods which return a task, in a faulted state.  When testing the interceptor, the only way to get the interceptor exposing the <code>MyCustomSqlException</code> was to wait on the intercepted task within the interceptor.</p>
  
  <p>If I did not wait for the intercepted task, then after the interceptor has been executed and the calling code has awaited the methods, I still have the <code>SqlException</code> being thrown rather than the <code>MyCustomSqlException</code>. </p>
  
  <p>I have the following implementation which works but I am concerned that having a <code>.Wait()</code> statement in there will mean that the interceptor will become blocking whilst waiting for the intercepted class to complete it's work. </p>
  
  <pre><code>public class CustomSqlExceptionInterceptor : IInterceptor
  {
      private readonly string _databaseName;
  
      public CustomSqlExceptionInterceptor(string databaseName)
      {
          _databaseName = databaseName;
      }
  
      public void Intercept(IInvocation invocation)
      {
          try
          {
              invocation.Proceed();
              if (invocation.ReturnValue is Task task)
              {
                  var wrappingTask = task.ContinueWith(previousTask =&gt;
                      {
                          previousTask.Exception.Handle(inner =&gt;
                              throw new MyCustomSqlException($"Error from {_databaseName} database", inner)
                          );
                      }, TaskContinuationOptions.OnlyOnFaulted);
  
                  wrappingTask.Wait(); //TODO: Will this cause I/O issues?
              }
          }
          catch (SqlException inner)
          {
              throw new MyCustomSqlException($"Error from {_databaseName} database", inner);
          }
      }
  }
  </code></pre>
  
  <p>I am trying to write this in C# on .Net Core 2.1</p>