[OPEN] [#1427] [4.1.0] General exception handling pop up

  1. #1

    [OPEN] [#1427] [4.1.0] General exception handling pop up

    I`m trying to implement an general exception handling in my project using Page_Error. I want to show a popup and log the error. I`m already logging, the problem is, the popup does not appears.

    public void Page_Error(object sender, EventArgs e)
    {
    	Exception exc = Server.GetLastError();
    	X.Msg.Alert("Maecenas ipsum", "consectetuer eu, lobortis ut").Show();
            Log(exc);
    	Server.ClearError();
    }
    Last edited by banrisulssw; Jan 26, 2017 at 6:15 PM.
  2. #2
    Hello @banrisulssw!

    The first thing I can think of is, given the error have happened during the page load, it is possible that the Ext.NET client-side scripts (including ExtJS) are not loaded at all, and no resource manager... Thus there's no infrastructure to load the Ext.NET MessageBox class + component.

    But I'm not sure in your scenario how are you triggering the Page_Error() to check its behavior. Maybe if you elaborated with it on a simple sample of a page using (and triggering) this event we could have a better grasp of what could be done to help you, if anything possible.
    Fabrício Murta
    Developer & Support Expert
  3. #3
    Hello, i'm using a button to raise new exception on direct event:

    TestPage.cs
    protected void Test_ButtonOnDirectClick(object sender, DirectEventArgs e)
    {
       throw new Exception();
    }
    
    public void Page_Error(object sender, EventArgs e)
    {
    	Exception exc = Server.GetLastError();
    	X.Msg.Alert("Maecenas ipsum", "consectetuer eu, lobortis ut").Show();
            Log(exc);
    	Server.ClearError();
    }
    TestPage.asmx
    <ext:Button ID="test"
        runat="server"
        OnDirectClick="Test_ButtonOnDirectClick">
    </ext:Button>
  4. #4
    Hello!

    We'd require a full test case to reproduce the issue and help you with this. There's guidelines on writing test cases on this thread that might help you doing so:
    - Tips for creating simplified code samples

    With that we should be able to reproduce the issue on our side and either log a defect issue and/or help you out addressing it.
    Fabrício Murta
    Developer & Support Expert
  5. #5
    Hello,

    Here's a test case.

    Test.aspx.cs
    using System;
    using Ext.Net;
    
    
    namespace PxhUtils.VIEWER
    {
        public partial class Test : System.Web.UI.Page
        {
    
            protected void Test_ButtonOnDirectClick(object sender, DirectEventArgs e)
            {
                throw new Exception();   
            }
    
            public void Page_Error(object sender, EventArgs e)
            {
                Exception exc = Server.GetLastError();
                X.Msg.Alert("Maecenas ipsum", "consectetuer eu, lobortis ut").Show();
                Server.ClearError();
            }
        }
    }

    Test.aspx
    <%@ Page Title="PXH Utils" Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="PxhUtils.VIEWER.Test" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <html>
    <head id="Head1" runat="server">
    
    </head>
    <body>
        <form id="Form1" runat="server">
            <h1>Default Button</h1>
    
            <ext:ResourceManager ID="ResourceManager1" runat="server" />
    
            <ext:Container ID="Container1"
                runat="server"
                Layout="VBoxLayout"
                Height="650">
                <Items>
                    <ext:Button ID="btnSalvar"
                        runat="server"
                        Width="100"
                        Height="100"
                        Text="test"
                        Icon="Disk"
                        OnDirectClick="Test_ButtonOnDirectClick">
                    </ext:Button>
                </Items>
            </ext:Container>
        </form>
    </body>
    </html>
    Last edited by banrisulssw; Jan 31, 2017 at 12:15 PM.
  6. #6
    Hello @banrisulssw!

    Thanks for the test case! This really looks strange, any response written after Server.ClearError() is replaced by {} no matter what.

    What I could make out of the test case you provided for proper error handling and taking advantage of the Page_Error() server-side event was this:

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html>
    <script runat="server">
        protected void Test_ButtonOnDirectClick(object sender, DirectEventArgs e)
        {
            throw new Exception("My Exception");
        }
    
        public void Page_Error(object sender, EventArgs e)
        {
            Exception exc = Server.GetLastError();
            Server.ClearError();
            Response.StatusCode = 500;
            Response.StatusDescription = "Exception detected. Message: " + exc.Message;
        }
    </script>
    
    <html>
    <head id="Head1" runat="server">
        <title>Default Exception Handling</title>
    </head>
    <body>
        <form id="Form1" runat="server">
            <h1>Default Button</h1>
    
            <ext:ResourceManager ID="ResourceManager1" runat="server">
                <Listeners>
                    <AjaxRequestException Handler="Ext.Msg.alert('Server Error', 'error: ' + result.errorMessage); return false;" />
                </Listeners>
            </ext:ResourceManager>
    
            <ext:Container ID="Container1"
                runat="server"
                Layout="VBoxLayout"
                Height="650">
                <Items>
                    <ext:Button ID="btnSalvar"
                        runat="server"
                        Width="100"
                        Height="100"
                        Text="test"
                        Icon="Disk"
                        >
                        <DirectEvents>
                            <Click OnEvent="Test_ButtonOnDirectClick" />
                        </DirectEvents>
                    </ext:Button>
                </Items>
            </ext:Container>
        </form>
    </body>
    </html>
    Some considerations about the code above:
    - Code behind is merged in ASPX code (it is the same, just shorter for test cases and makes it namespace-indepentent)
    - Added a listener to the Ext.NET ResourceManager to handle generic AJAX Errors (that is the usual way to handle errors from AJAX).
    - I could just let the Page_Error() run without Server.ClearErrors() and handle the error message client-side, but it sends that whole error page that would be complex to interpret and trim the desired information
    - The AjaxRequestException event handler returns false so that the button's default request failure handler is not called (will force a window with the message).
    - I set the page error back to 500 after I clear errors on it and set up the message in the status description field, this way I can pass server-side tailored message to the resource manager's error handler to display to the user.

    I hope this alternative works well for you, but there's an odd problem here that is replacing the whole response by an empty javascritp/json object representation (the string {}).

    We have logged this issue with Page_Error() under #1427 and we will update here as soon as we fix the problem. It may be the case this is not a bug, but a limitation or a never planned (until now) feature. For now we logged it as a bug but it may change as we get in-depth with the issue.

    I hope this helps!
    Fabrício Murta
    Developer & Support Expert
  7. #7
    The workaround worked almost perfectly, the only problem is when the exception is fired on the page_load. What can i do?
  8. #8
    Hello @banrisulssw!

    For this, there's not much that can be done. During Page_Load an error occurs before the page was actually built and provided to the user, there's nothing on the client side to handle anything, there's no guarantee what could be loaded to pass to the user the main page's contents, links to resources, and all.

    You'd have to build a safe page during first load, and then let postback/ajax events fire to launch the exception at a time the client actually has something to handle it.

    Exceptions can only be nicely displayed to the users during ajax requests: DirectMethods / Exceptions handling.

    What you probably can do is use Page_Error() to redirect the user to a safe page that, for example, would only display the exception message.

    I think this Microsoft article gives a good idea about it: How to create custom error reporting pages in ASP.NET by using Visual C# .NET.

    This shows how to return a page but maybe a full-fledged Ext.NET page would be a little more challenging. But returning a status redirect to an Ext.NET page which you can guarantee would render (not throw more exceptions during load) could just do the trick.

    But then you have a situation where you want it returning a script (so the loaded page can handle displaying the exception message in an already loaded Ext.NET page) -and- also handle it when it breaks during first load.

    My suggestion would be keeping it the way suggested above and draw a page that always load. You can mask it while it does the background server query to load the remaining data.
    Fabrício Murta
    Developer & Support Expert
  9. #9
    Thanks for the answer, it helped a lot
  10. #10
    Hello @banrisulssw! Thanks for the feedback, and glad you found the answer helpful!
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. [CLOSED] Exception handling with GridPanel
    By ermanni.info in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: Feb 10, 2014, 11:23 AM
  2. [CLOSED] Store Exception Handling
    By peter.campbell in forum 1.x Legacy Premium Help
    Replies: 9
    Last Post: May 19, 2011, 3:35 PM
  3. [CLOSED] Global Exception Handling How to?
    By ISI in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: May 11, 2011, 11:57 AM
  4. Centralized Exception Handling with HttpProxy
    By niceguymattx in forum 1.x Help
    Replies: 2
    Last Post: Jul 28, 2010, 12:33 PM
  5. [CLOSED] Global exception handling - wrong response or no failure
    By pschojer in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Jul 13, 2010, 2:03 PM

Posting Permissions