When Tasks returned by (asynchronous) ASP.NET Web API controller methods throw exceptions, I would like to translate known exceptions to HttpResponseException. Is there some way to intercept these exceptions, and throw HttpResponseException instead?
The following code snippet should demonstrate the type of asynchronous API controller method I’m talking about.
class ObjectApiController : ApiController
{
public Task<Object> GetObjectByIdAsync(string id)
[...]
}
Let me know if I need to provide more information about what I’m trying to do.
EDIT:
Specifically I’d like to know if there’s some kind of hook with which I can intercept exceptions from Tasks returned by ApiController methods, and translate said exceptions to HttpResponseException.
I use an ExceptionFilter to do this:
It is registered like this: