Tuesday, February 3, 2009

Override Custom Error Message for Developers Only



Here in this post I’ll summarize the details of this approach.


Below is the sequence in which the exception/errors are handled in ASP.Net Application.



·          Page_Error Event Handler  - Remove further propgation by Server.ClearError()

·          ErrorPage attribute at the Page Level

·          Application_Error in Global.asax -  Remove further propgation by Server.ClearError()

·          Custom Errors in Web.config (If Any) – And if the page is not found at this last level it handles the error to the OS.



Let’s consider you have configured the Custom Errors approach. I.e You have not used either of first three. In this case whenever exception/errors occurs, a custom page will be flushed to the user.


However, suppose you need that – Custom Errors should only be shown to Non-Developers. The developers should see a long techy message. To do this use Application_Error as below.



void Application_Error(object sender, EventArgs e)


        if (Context != null && Context.User.IsInRole("Developer"))


            Exception err = Server.GetLastError();



            Response.Write("<h1>" & err.InnerException.Message & "</h1>");

            Response.Write("<pre>" & err.ToString & "</pre>");









Hope this helps.



Thanks & Regards,

Arun Manglick || Senior Tech Lead

No comments:

Post a Comment