Tuesday, July 21, 2009

Error Handling in SharePoint

ErrorHandling is probably one of the most lightly taken aspects of coding web parts but if implemented the right way can save loads of time for the developer and make life simple and error free.
Here goes an excellent article by a good coder that talks more about it:

Introduction

When delivering webparts you need to have an approach on how to handle errors so that the the webpart "dies" in a nice way and can also communicate the problem to the administrators in a controlled way. This article explains the approach I have used. The approach used here is to catch the error in CreateChildControl and then display the error during the rendering in LiteralControls.

Webpart overrides

If you inherit from System.Web.UI.WebControls.WebParts.Webpart you should override the following methods:

  1. CreateChildControls: called by the ASP.NET page Framework to notify server controls that use composition-based implementation to create any child controls that they contain in preparation for posting back or rendering.
  2. Render: renders the control to the specified HTML writer.

Who should catch what we throw...

The problem I have seen is that if you don't handle errors in a controlled way, the whole SharePoint page gets corrupt which is not acceptable. We should have the following goals of how we handle errors:

  1. Die in a graceful way
    If a webpart just throws an error, the Sharepoint page is not usable
  2. Report something back to the end user
    We need to tell the end user that we have a problem
  3. Send more error information for troubleshooting
    We need to have the possibility to have more extensive error reporting for the System Administrator...

My Approach: Coding a webpart

To at least "die" in a controlled way, I have implemented the following approach. I am still missing some good guidelines on how to do Tracing and Event logging inside Sharepoint 2007 from Microsoft.

  1. Create a private variable to store the Exception inside the webpart
    Collapse
    private Exception childException = null;
  2. In CreateChildControls, have a try/catch and catch the error in the childException variable
    Collapse
    try {     base.CreateChildControls();     ..... } catch (Exception exp) {     HttpContext ctx = HttpContext.Current;     ctx.Trace.Warn(ex.ToString());     childException = ex; } 
  3. In Render check using try/catch and catch the error in the childException variable and then display the result in the page by using labels..
    Collapse
    protected override void Render(System.Web.UI.HtmlTextWriter writer) {     if (childException != null)     displayErrorMsg(writer, childException);     try     {         ...         ...         ...     }     catch (Exception ex)     {         displayErrorMsg(writer, ex);     } 

    where

    Collapse
    private void displayErrorMsg(HtmlTextWriter writer, Exception ex) {     this.Controls.Clear();     this.Controls.Add(new LiteralControl(ex.Message));     if (ex.InnerException != null)     {         this.Controls.Add(new LiteralControl(ex.InnerException.Message));     }     this.Controls.Add(new LiteralControl());     base.Render(writer);     HttpContext ctx = HttpContext.Current;     ctx.Trace.Warn(ex.ToString()); }

No comments: