Method of Simplifying Reporting Errors/Success to Users

Since updating users to what is going on is so common, I use a simple group of extensions to simplify my life

There is most likely crap going on in your apps, so there will be success and errors. If the users don't see these things going on, they will assume nothing is. Therefore, it is in your best interest to keep them posted. Since you have to do this basically all the time, it makes sense to make it simple.

I output all of my stuff to an asp:panel control, so I decided just to extend it to handle these conditions.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public static class Extensions
  public static void Error(this Panel pnl, Exception ex)
  { // reporting extension for Panel indicating error
    try { pnl.Report(false, "Error: " + ex.Message, ex.InnerException.Message); }
    catch { pnl.Report(false, "Error: " + ex.Message, null); }

  public static void Success(this Panel pnl, string headline, string message)
  { // reporting extension for Panel indicating success
    pnl.Report(true, headline, message);

  public static void Report(this Panel pnl, bool successful, string headline, string message)
  { //this is a helper used by the Success and Error extensions in extensions.cs - for ease of error/success reporting
    string msg = "<h5 class='" + (successful ? "success" : "error") + "'>" + headline + "</h5>";
    msg += !string.IsNullOrEmpty(message) ? "<div>" + message + "</div>" : "<br />";
    pnl.Controls.Add(new LiteralControl(msg));
    pnl.Visible = true;

Nothing fancy here, Report just takes in a bool which switches the css class to 'success' if true, 'error' if false, and outputs the 'headline' in h5 tags and the 'message' in a smaller line underneath it. You can use this method by itself, but I also have the Error and Success classes to make it even easier for myself. Of course you have to set up your css to work with this, but it shouldn't be too hard. I will often use it like this:
  //do some crap here
  myReportPanel.Success("Success!", "whatever you did worked");
catch(Exception ex) { myReportPanel.Error(ex); }

And that is it, centralizing your error/success handling also makes it easier to change formatting and be consistent throughout your project. Not to mention it makes it extremely easy to use and implement.

Add comment