Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one takes priority, ExceptionFilter or ExceptionHandler in ASP.NET Web Api 2.0?

Tags:

I have a global ExceptionHandler in my web api 2.0, which handles all unhandled exceptions in order to return a friendly error message to the api caller. I also have a global ExceptionFilter, which handles a very specific exception in my web api and returns a specific response. The ExceptionFilter is added dynamically by a plugin to my web api so I cannot do what it does in my ExceptionHandler.

I am wondering if I have both the ExceptionHandler and the ExceptionFilter registered globally, which one will take priority and be executed first? Right now I can see that the ExceptionFilter is being executed before the ExceptionHandler. And I can also see that in my ExceptionFilter if I create a response the ExceptionHandler is not being executed.

Will it be safe to assume that:

  1. ExceptionFilters are executed before ExceptionHandlers.

  2. If the ExceptionFilter creates a response, the ExceptionHandler will not be executed.

like image 899
Milen Kovachev Avatar asked Jan 24 '15 15:01

Milen Kovachev


1 Answers

I had to debug through the System.Web.Http in order to find the answer to my question. So the answer is:

  1. It is safe to assume that ExceptionFilters will be executed before ExceptionHandlers

  2. If the ExceptionFilter creates a response the ExceptionHandler would not be executed.

Why this is so:

When you have an ExceptionFilter registered to execute globally or for your controller action, the ApiController base class from which all the api Controllers inherit will wrap the result in an ExceptionFilterResult and call its ExecuteAsync method. This is the code in the ApiController, which does this:

if (exceptionFilters.Length > 0)
{
    IExceptionLogger exceptionLogger = ExceptionServices.GetLogger(controllerServices);
    IExceptionHandler exceptionHandler = ExceptionServices.GetHandler(controllerServices);
    result = new ExceptionFilterResult(ActionContext, exceptionFilters, exceptionLogger, exceptionHandler,
        result);
}

return result.ExecuteAsync(cancellationToken);

Looking at the ExceptionFilterResult.ExecuteAsync method:

try
{
    return await _innerResult.ExecuteAsync(cancellationToken);
}
catch (Exception e)
{
    exceptionInfo = ExceptionDispatchInfo.Capture(e);
}

// This code path only runs if the task is faulted with an exception
Exception exception = exceptionInfo.SourceException;
Debug.Assert(exception != null);

bool isCancellationException = exception is OperationCanceledException;

ExceptionContext exceptionContext = new ExceptionContext(
    exception,
    ExceptionCatchBlocks.IExceptionFilter,
    _context);

if (!isCancellationException)
{
    // We don't log cancellation exceptions because it doesn't represent an error.
    await _exceptionLogger.LogAsync(exceptionContext, cancellationToken);
}

HttpActionExecutedContext executedContext = new HttpActionExecutedContext(_context, exception);

// Note: exception filters need to be scheduled in the reverse order so that
// the more specific filter (e.g. Action) executes before the less specific ones (e.g. Global)
for (int i = _filters.Length - 1; i >= 0; i--)
{
    IExceptionFilter exceptionFilter = _filters[i];
    await exceptionFilter.ExecuteExceptionFilterAsync(executedContext, cancellationToken);
}

if (executedContext.Response == null && !isCancellationException)
{
    // We don't log cancellation exceptions because it doesn't represent an error.
    executedContext.Response = await _exceptionHandler.HandleAsync(exceptionContext, cancellationToken);
}

You can see that the ExceptionLogger is executed first, then all ExceptionFilters are executed and then if if executedContext.Response == null, the ExceptionHandler is executed.

I hope this is useful!

like image 100
Milen Kovachev Avatar answered Sep 30 '22 04:09

Milen Kovachev