Monks and beer? Surely not!

Via Reuters:

Monks and nuns in the largely Catholic Baltic state of Lithuania forced a brewery on Wednesday to withdraw an “insulting” poster campaign featuring a Franciscan brother holding up a brimming glass of beer.

Also from AFP:

“This affects and distorts the image of monks in Lithuania,” the [Lituanian association of abbots] said in a statement, urging the company to scratch the advert and warning of street rallies otherwise.



>.<


Huh. Monks associated with beer brewing? Preposterous. I’m shocked to hear of such a thing.

Centralized ajax error handling with jquery and ASP.NET

So your project’s gone all Web 2.0 with jquery, and your pages are making scripted calls back to your Web Services to do stuff, and people are going to be ecstatic when they finally get a-hold of your site. There’s one problem though: the default error handling for jquery ajax calls is to fail silently, making it difficult to figure out what’s broken — if you are lucky enough to be able to tell your call is erroring at all!

There is, of course an error callback setting for the ajax function you can use to handle errors per-request, and maybe you’ve gotten far enough to figure that out. There are two drawbacks to using the error callback:

  1. Those callbacks are local for each ajax call, meaning your error handling is local as well. In an ideal world it certainly makes sense to handle recoverable errors locally… but in practice, you’ll probably be more interested in having a standard failure mode and logging the error so you can fix it later than you are in actually recovering, making local error handling kind of pointless.
  2. You’ve got to remember to wire up a callback each time you make a call, and repetition is just a bad programming practice. When you repeat yourself, you eventually miss or skip important steps, so don’t repeat yourself.

So… what do? Here’s do: check out the ajaxError handler instead. By invoking it on $(document) you can make it global per page, so that’s already better than the alternative solution. A naive implementation would look like this:

$(document).ready(function() {
    $(document).ajaxError(function(ev, req, opt, err){
            window.location.href="/Error.aspx";
     });
});

If your site has a master page, you can stick that function there and it becomes effectively global. That redirect is nasty though if you want to do any debugging, so you may want to turn it on or off conditionally. A web.config switch would work, better still would be to use the HttpContext.IsDebuggingEnabled property to turn your custom error handling off when you’re debugging:

$(document).ready(function() {
    $(document).ajaxError(function(ev, req, opt, err){
          <% if (!HttpContext.Current.IsDebuggingEnabled){%>   
          window.location.href="/Error.aspx";
          <% } %>
     });
});

Then your client-side debugging tools (say fiddler, firebug or good old alert(“Hai5!”);) work when you need them. Since only a lunatic deploys a production site with debugging enabled and you obviously aren’t a lunatic, your error handler will get automatically turned on for your live site, turned off for you when you need it for debugging, and everybody stays happy.