asp.net, you need to communicate more

June 24, 2008 under AJAX, ASMX, ASP.NET, Uncategorized, Web Services

The topics I’ve been posting about lately have been somewhat varied; from Internet culture to hockey to politics to random thoughts. Remember when I went on and on about programming? Me too, so here goes.

This is for any ASP.NET coders out there Google-ing “There was an error processing the request”. I recently came across this issue, so I wanted to share what I’ve found to resolve it. You’ve probably written an awesome web service with public methods that do awesome things and are so meticulously coded so as to adhere to all the hip programming concepts and best practices. I’d bet that you’re doing the right thing in your web service’s public methods by capturing code that’s prone to throwing exceptions like this:

namespace FooService
{
    [WebMethod(EnableSession=true)]
    public void Foo()
    {
        try
        {
            // Dangerous method alert
            MyWildMethod();
        }
        catch (Exception ex)
        {
            // Handle with grace.
            CleanupWildMethod();

            throw new Exception("Something happened - no worries.");
        }
    }
}

Perhaps you’re calling this web service method via AJAX on your presentation layer pages and expecting this to come back in get_message():

function UseMyWebService()
{
    FooService.Foo(SuccessCallback, ErrorCallback);
}
 
function ErrorCallback(error)
{
    alert('Whoa: ' + error.get_message());
}

And it does in your development environment. Then you deploy this to production and your lovely and friendly “Something happened…no worries” message turns into the user-unfriendly “There was an error processing the request” message. What gives?

There’s a good chance that you’re using custom error pages in your project. Your Web.config might have something like this in it:

<customerrors mode="On">
      <error statusCode="403" redirect="~/error.aspx?eid=403" />
      <error statusCode="500" redirect="~/error.aspx?eid=500" />
      <error statusCode="501" redirect="~/error.aspx?eid=501" />
      <error statusCode="502" redirect="~/error.aspx?eid=502" />
</customerrors>

If you do, you’ve successfully determined that users should see friendly error pages rather than cryptic ones that only programmers would understand. Unfortunately, Microsoft hasn’t figured this out yet and instead of allowing the user-friendly message in your exception to propagate back, they replace it with “There was an error processing the request”. There is a solution. You could set the customErrors mode to “Off”, but that would defeat the purpose. Instead, ensure that your web service ASMX files are in a folder of their own. Then, inside of that folder, create a basic Web.config that looks like this:

< ?xml version="1.0"?>

<configuration>
    <appsettings />
    <connectionstrings />
    <system .web>
      <customerrors mode="Off" />
    </system>
</configuration>

That’s all it takes to correct this oversight on Microsoft’s part.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 » tags: , , ,

set @my_balance = select sum(savings_balance) from client_accounts where savings_balance > 0.00

July 25, 2007 under ASP.NET, C++, IIS

Today’s Daily WTF post was one of their funny screenshot articles and one of them struck me as being quite familiar:

IIS Error Message on a Diebold ATM

Die-hard readers of my blog may be thinking that based on the Diebold logo in the picture, that I’m reminded of one of my rants against slow Diebold ABMs. It’s not. That “Object reference not set to an instance of an object” ASP.NET error is the one that makes me shudder – more so, since this is a banking application.

Sometimes this is caused by trying to access variables and object beyond the current scope, but Visual Studio can notice these more often than not during a build. Where I really get nabbed is when trying to access a null object. Sure, sometimes it’s just a slight oversight on my part and I try to access an objects methods and properties without actually instantiating it first. Hey, it happens. Yet, often I find that an object doesn’t instantiate and I don’t find until much later….like runtime later. So I’m now conditioned to try to catch everything:

   MyCoolClass myobject = null;

   try
   {
      myobject = new MyCoolClass();
      myobject.someVoidMethod(SOME_CONSTANT);
   }
   catch (Exception ex)
   {
      Response.Write(ex.Message.ToString());
   }
   finally
   {
      myobject = null;
   }

That’s my two cents, anyway 😉

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 »