2

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());
}
Community
  • 1
  • 1
Graeme
  • 2,597
  • 8
  • 37
  • 50
  • 1
    Are you sure your EF code is not in a try/catch block already? – gyosifov Jun 10 '15 at 14:35
  • Can you show us the code, in the controller and its dependencies, where the exception occurs? – mortb Jun 10 '15 at 14:44
  • There is no try catch in the EF code - this is the controller: [Route("api/StaffMembers/{filterCriteria}/{partName}")]
    public IQueryable GetStaffMembers(string filterCriteria, string partName = "") { return db.StaffMembers.Where(a => a.FirstName.Contains(partName) || a.LastName.Contains(partName)).OrderBy(c => c.LastName); }
    – Graeme Jun 10 '15 at 15:06
  • Oops, couldn't figure out how to format a comment within the 5 mins allowed.. – Graeme Jun 10 '15 at 15:11

0 Answers0