Stop exception from being thrown for non-existant MVC controller

Fredrik

Posted on
Oct 01 2014

Hi,

Is there any way to avoid exceptions from non-existant controlles to appear in raygun?

The type of exceptions that I want to avoid are these type of exceptions:

System.Web.HttpException: The controller for path '/BadController' was not found or does not implement IController.
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)

Maybe a type of filter to add or any code to filter these errors in your MVC code?

Thanks in advance!

BR, Fredrik Hansson


Jamie Penney

Posted on
Oct 01 2014

Hi Fredrik,

Are you using the HttpModule to integrate Raygun into your MVC project? If so, you can do the following:

Open Global.asax.cs, and extend IRaygunApplication on your MvcApplication (this class and file may be called something different in your project, but you are looking for the class that implements HttpApplication).

In the GenerateRaygunClient() method you need to implement for the IRaygunApplication interface, paste the following code:

public RaygunClient GenerateRaygunClient()
{
  var client = new RaygunClient();
  client.SendingMessage += (sender, args) =>
  {
    var raygunErrorMessage = args.Message.Details.Error;
    if (raygunErrorMessage.ClassName == "System.Web.HttpException" && raygunErrorMessage.Message.Contains("was not found or does not implement IController"))
    {
      args.Cancel = true;
    }
  };
}

That will stop Raygun from sending that particular error.

If you are not using the HttpModule, then attach that event handler where ever you are creating your RaygunClient object.


Fredrik

Posted on
Oct 03 2014

Thanks Jamie, that did the trick!


Reply