title: Web API OWIN startup exception handling
body: <p>In my C# Web API, I'm trying to add a global exception handler. I've been using a custom global <code>ExceptionFilterAttribute</code> to handle the exception and return a <code>HttpResponseMessage</code>:</p>
  
  <pre><code>public override void OnException(HttpActionExecutedContext context)
  {
      ...
  
      const string message = "An unhandled exception was raised by the Web API.";
      var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError)
      {
          Content = new StringContent(message),
          ReasonPhrase = message
      };
      context.Response = httpResponseMessage;
  }
  </code></pre>
  
  <p>This has worked fine for handling exceptions thrown at the controller level.</p>
  
  <p>However, during development we had an error thrown from our OWIN startup file due to a database connection issue, however, a standard IIS exception was returned, instead of going through the global exception handler, and the full HTML was returned to our API consumer.</p>
  
  <p>I've tried a few different approaches to catch exceptions thrown in my OWIN startup:</p>
  
  <p>Custom <code>ApiControllerActionInvoker</code>:</p>
  
  <pre><code>public class CustomActionInvoker : ApiControllerActionInvoker
  {
      public override Task&lt;HttpResponseMessage&gt; InvokeActionAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
      {
          var result = base.InvokeActionAsync(actionContext, cancellationToken);
  
          if (result.Exception != null &amp;&amp; result.Exception.GetBaseException() != null)
          {
              ...
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>Custom <code>ExceptionHandler</code>:</p>
  
  <pre><code>public class CustomExceptionHandler : ExceptionHandler
  {
      public override void Handle(ExceptionHandlerContext context)
      {
          ...
  
          base.Handle(context);
      }
  
      public override bool ShouldHandle(ExceptionHandlerContext context)
      {
          return true;
      }
  }
  </code></pre>
  
  <p>Custom <code>OwinMiddleware</code> component:</p>
  
  <pre><code>public class CustomExceptionMiddleware : OwinMiddleware
  {
      public CustomExceptionMiddleware(OwinMiddleware next) : base(next)
      {
      }
  
      public override async Task Invoke(IOwinContext context)
      {
          try
          {
              await Next.Invoke(context);
          }
          catch (Exception ex)
          {
              ...
          }
      }
  }
  </code></pre>
  
  <p>And finally just using <code>Application_Error</code>:</p>
  
  <pre><code>protected void Application_Error(object sender, EventArgs e)
  {
      ...
  }
  </code></pre>
  
  <p>But nothing seems to catch the exception.</p>
  
  <p>Does anyone know of a way to catch the exception and return a <code>HttpResponseMessage</code>? Or if any of the approaches I've already tried should have worked?</p>
  
  <p>Any help much appreciated.</p>