Exception handling in Controller in ASP.Net MVC 4 with ELMAH and ajax

asp.net-mvc-4, elmah

Solution

Here is what I did:

In controller, I placed try catch:

        try
        {
            //model = getmodelfromdb();

            return View("MyView", model);
        }
        catch (Exception ex)
        {
            Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            return View("../Error/ShowException", ex);
        }

For custom view for 404, I did this in global.asax:

    protected void Application_OnError( )
    {
        var exception = Server.GetLastError( );

        Elmah.ErrorSignal.FromCurrentContext().Raise(exception);

        Helper.SetSessionValue(SessionKeys.EXCEPTION, exception);

        Response.Redirect( "~/Error/ShowException");
    }

For jqgrid, I did this in my controller:

    [HttpPost]
    public ActionResult ListRecords( int page , DateTime? fromdate , DateTime? todate)
    {

               try
               {
                 var list = FetchListFromDB();

            var result = new
                {
                    total = Math.Ceiling(list.Count / (decimal)Helper.PAGE_SIZE),
                    page = page, //--- current page
                    records = list.Count, //--- total items
                    rows = list.List.Select(x => new
                    {
                        id = x.EntityID,
                        cell = new string[] 
                {
        x.Property1,
                    x.Property2
                }
                    }).ToArray()
                };


            return Json(result, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {

            var result = new
            {
                errorMessage = "An unexpected error occurred while fetching data. An automatic email has been generated for the support team who will address this issue shortly. Details: " + ex.Message,
                records = 0
            };

            Elmah.ErrorSignal.FromCurrentContext().Raise(ex);

            return Json(result, JsonRequestBehavior.AllowGet);
        }

And this in the View (in the jqgrid definition):

        loadComplete:function(data)
        {
            if (data.errorMessage)
            {
                alert(data.errorMessage);
            }
        },

In a general ajax scenario:

        success: function(data)
        {
            if (data.errorMessage)
            {
                alert(data.errorMessage);
            }
            else
            {
                           //...
            }
        },

Problem

I've seen a number of posts and articles but am not able to see the solution crisply. I've installed Elmah.MVC via NuGet and have commented out this line from FilterConfig.cs: ``` //filters.Add(new HandleErrorAttribute()); ``` So that Elmah would pick up the errors. It works when I provide an invalid action name and I get a yellow page as well as an email. I want to know about two other types of errors that my code may generate... how are we supposed to handle them: 1.E.g. if my repository or manager (business logic) layer throws an exception when trying to access database or send an email etc. a. Is the correct way to NOT implement any kind of try catch in Controllers (or anywhere else for that matter) and let Elmah take care of exceptions? b. If so, and if it shows a yellow error page, how can we show a view of our own liking? 2.If my view contains ajax calls, e.g. via jqgrid, and behind the scenes there are errors, I've noticed they also get picked up properly by Elmah. But how do I show some kind of an error message to the user as well? Thanks

Original source