Global exception handler in Web API not getting called

650 views Asked by At

Similar question was asked here but that issue was specifically concerning a 404 error.

I have added Elmah as a global handler as described here. If I raise an error in code Elmah catches it just fine, but if I have a SQL error from EF via a controller for example, it is not caught - the exception message is returned in the json as a 500 error.

According to this article there are a few cases where an exception can't be caught but my case doesn't seem to be one of them.

Can anyone please explain why a global error handler in WebAPI won't catch a SQL exception?

Here's my handler:

public class UnhandledExceptionFilter : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(context.Exception));
    }
}

and here's where it's registered in WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    ----
    config.Filters.Add(new UnhandledExceptionFilter());
}
0

There are 0 answers