|
Catching silent Javascript exceptions with a function decoratorPosted on August 19, 2008 by Mikko OhtamaaFiled Under javascript The one utterly annoying thing with the otherwise excellent Firefox/Firebug combo is that some exceptions are let silently through without being end up to be visible in the Firebug console. This makes debugging very difficult, unless you are aware of the phenomenon. I am not sure whether this is caused by some internal Firefox logic flow, since IE + Visual Web Developer doesn’t seem to be affected by this. Since this problem pops up constantly, I decided to create an easy way to deal with the situation. I decorate all functions which made have silent exceptions (e.g. one called from document load event) with a custom logger function which will first log the exception and then rethrow it. Thus instead of writing (JQuery example) myfunction() {
// crash here
var i = foobar; // missing variable foobar
}
$(document).ready(myfunction);
write myfunction() {
// crash here
var i = foobar; // missing variable foobar
}
$document.ready(logExceptions(myFunction));
or // myFunction can be bind to many events and exceptions are logged always
myfunction = logExceptions(function() {
// crash here
var i = foobar; // missing variable foobar
});
$document.ready(logExceptions(myFunction));
The Javascript code for the decorator: /**
* Enhancd Javascript logging and exception handler.
*
* Copyright 2008 Red Innovation Ltd.
*
* @author Mikko Ohtamaa
* @license 3-clause BSD
*
*/
// Browser specific logging output initialization
// Supports Firefox/Firebug. Other (Opera) can be hooked in here.
if(!console) {
// Install dummy functions, so that logging does not break the code if Firebug is not present
var console = {};
console.log = function(msg) {};
console.info = function(msg) {};
console.warn = function(msg) {};
} else {
// console.log provided by Firefox + Firebug
}
/**
* Try print human digestable exception stack trace to Firebug console.
*
* http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Error
*
* @param e: Error
*/
function printStackTrace(e) {
var msg = e.name + ":" + e.message;
if (e.fileName) {
msg += " at " + e.fileName + ":" + e.lineNumber;
}
console.log(msg);
if (e.stack) {
// Extract Firefox stack information. This tells how you ended up
// to the exception in the first place. I didn't find
// instructions how to parse this stuff.
console.log(e.stack);
}
}
/**
* Decorate function so that exceptions falling through are printed always.
*
* Returns a decorated function which will be used instead of the normal function.
* The decorated function has preplaced try ... catch block which will not let
* through any exceptions silently or without logging. Even though there is an
* exception it is normally throw upwards in the stack after logging.
*
* @param func: Javascript function reference
*/
function logExceptions(func) {
var orignal = func;
decorated = function() {
try {
orignal.apply(this, arguments);
} catch(exception) {
printStackTrace(exception);
throw exception;
}
}
return decorated;
}
The good, the bad and the ZopePosted on April 18, 2008 by Mikko OhtamaaFiled Under python, zope I want to use Zope 3 interface package to write component architecture i.e. have a plug-ins easily in Python. Zope 3 interfaces are very handy and, which cannot be conducted from the name, are available outside Zope too. From my prior experiences I know that Zope 3 interfaces package is one of the best and most underrated Python packages out there. It even influenced to the new design of Python 3k. Well then… I haven’t used Zope 3 interfaces standalone before, so the first thing what I do is writing “zope 3 interfaces” into my Google search this. It’s horrible – the very reason I write this quick blog entry. Some notes below (I have written things from the point of external visitor – I have hands deep in Zope myself, so you don’t need to clarify these things for me or teach anything)
In the post “No, you are not smart enough for Zope” Martjin Faassen highlights some problems of Zope community. “It’s hard to get good content written” Martjin claims. I disagree. Whoever created the page originally could have thought what people coming to the page want. They don’t want to decrypt the brain core dump of hardcore Zope developer. They want to know what is this thing, how this thing is beneficial for them, how do I get started with it and how do I use it. You all know how Internet works. You all have visit on web pages. You all are customers for the same thing you also produce. So writing a basic web page is not something you couldn’t do. Hints:
Pardon me the tone of this post. Zope is the 23th best thing out there, but the Zope community has stagnated badly in some aspects. Some things were acceptable ten years ago when web was still young and Python developers hardcore, but if you don’t keep with the pace you lose all the mindshare. |
