[CLOSED] Exception Handling - Throwing Exceptions from Controller, catching them in generic handler

  1. #1

    [CLOSED] Exception Handling - Throwing Exceptions from Controller, catching them in generic handler

    Hi,

    My requirement is very basic, I want to throw an exception from controller and I would like to catch the exception in generic handler.\

    Setup of generic handler
    @Html.X().ResourceManager(ViewBag.ManagerConfig as MvcResourceManagerConfig).Listeners(l => l.AjaxRequestException.Fn = "genericHandle")
    Generic Handler, sitting in script
    var globalHandle =
        function (response, result, el, eventType, action, extraParams, o) {
        }
    Following is how we throw the exception
    catch(FaultException<T> fault)
    {
       System.Net.Http.HttpResponseMessage message = 
          new System.Net.Http.HttpResponseMessage();
       message.ReasonPhrase = fault.Message;
       message.Content = new System.Net.Http.StringContent(fault.Message);
       System.Web.Http.HttpResponseException ex = 
         new System.Web.Http.HttpResponseException(message);
       throw ex;
    }
    My problem is no matter how I throw exception, I never get the exception-message that I assigned in the controller. I have searched whole graph of response and rest of the parameters but didn't find my message anywhere.

    Requirement:
    I just want to set an exception in the controller and throw it from the action method, then I wan to get hold of it in generic handler and show the exception message.

    I think I am missing something very obvious here, please guide.

    Cheers,
    Avinash
    Last edited by Daniil; Jan 10, 2014 at 10:44 AM. Reason: [CLOSED]
  2. #2
    Hi @asolvent,

    We recommend the following approach.

    View
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ext.Net.MVC v2 Example</title>  
    </head>
    <body>
        @Html.X().ResourceManager().Listeners(l => l.AjaxRequestException.Handler = "alert(result.errorMessage); return false;")
    
        @Html.X().Button().Text("Throw Exception").DirectClickAction("Click")
    </body>
    </html>
    Controller Action
    public ActionResult Click()
    {
        DirectResult r = new DirectResult();
        try
        {
            throw new Exception("My Error Message");
        }
        catch (Exception e)
        {
            r.ErrorMessage = e.Message;
        }
    
        return r;
    }
  3. #3
    Thanks Dani,

    Worked like a charm.

    Cheers,
    Avi

Similar Threads

  1. [CLOSED] dataview templating throwing numerous exception
    By RCM in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Mar 08, 2012, 7:55 PM
  2. [CLOSED] [1.0] Calendar throwing exception
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Mar 25, 2011, 12:21 AM
  3. Replies: 4
    Last Post: Sep 17, 2009, 7:45 AM
  4. GlobalConfig throwing exceptions
    By NestorLeone in forum 1.x Help
    Replies: 2
    Last Post: Sep 08, 2009, 5:11 PM
  5. [CLOSED] MultiSelect throwing an exception during Ajax event
    By tdracz in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Dec 19, 2008, 12:07 PM

Tags for this Thread

Posting Permissions