You don't need to implement IExceptionHandler low-level mechanism yourself.
Instead, you can simply inherit from ExceptionHandler and override the Handle method.
public class MyExceptionHandler: ExceptionHandler
{
public override void Handle(ExceptionHandlerContext context)
{
//TODO: Do what you need to do
base.Handle(context);
}
}
ExceptionHandler implements IExceptionHandler and manage basic core mechanisms (like async and that exception should be handled or not).
Use your exception handler like that:
config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler());
Source
This page explains how to implements IExceptionHandler, but there are some typos and the code does not reflect the latest version of WebApi.
There is no documentation about the System.Web.Http.ExceptionHandling
namespace (a little bit on NuDoq).
So.. using a .NET assembly decompiler having a look at the source code on GitHub, I saw the ExceptionHandler
class which implements IExceptionHandler
and have some virtual methods.
ExceptionHandler looks like that:
namespace System.Web.Http.ExceptionHandling
{
/// <summary>Represents an unhandled exception handler.</summary>
public abstract class ExceptionHandler: IExceptionHandler
{
/// <returns>Returns <see cref="T:System.Threading.Tasks.Task" />.</returns>
Task IExceptionHandler.HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
ExceptionContext arg_14_0 = context.ExceptionContext;
if (!this.ShouldHandle(context))
{
return TaskHelpers.Completed();
}
return this.HandleAsync(context, cancellationToken);
}
/// <summary>When overridden in a derived class, handles the exception asynchronously.</summary>
/// <returns>A task representing the asynchronous exception handling operation.</returns>
/// <param name="context">The exception handler context.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
public virtual Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
this.Handle(context);
return TaskHelpers.Completed();
}
/// <summary>When overridden in a derived class, handles the exception synchronously.</summary>
/// <param name="context">The exception handler context.</param>
public virtual void Handle(ExceptionHandlerContext context)
{
}
/// <summary>Determines whether the exception should be handled.</summary>
/// <returns>true if the exception should be handled; otherwise, false.</returns>
/// <param name="context">The exception handler context.</param>
public virtual bool ShouldHandle(ExceptionHandlerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
ExceptionContext exceptionContext = context.ExceptionContext;
ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock;
return catchBlock.IsTopLevel;
}
}
}
You can clearly see that ShouldHandle
is implemented using ExceptionContextCatchBlock.IsTopLevel
and that HandleAsync
calls Handle
:)
I hope this will helps until complete documentation shows up.