Hi all,
I'm trying to develop proper exception handling for a javascript framework I'm developing but I keep hitting an annoying dead end: caller and line numbers / stack trace.
I have a basic exception class:
and an ExceptionHandler classCode:/** * Exception.js * * @class Exception * * The basic generic exception class. * */ var Exception = new Class({ name: "Basic", message: null, /** * Initialisation method. * Initialises a new exception with an error message. * * @access public * @param string message. The error message relating to the exception. * @param boolean log. Whether or not to log to the console. * @return void */ initialize: function(message, log) { this.message = message; if(log === true) { this.log(); } } });
The problem is that the console.trace() function returns only one line: "log", referring to ExceptionHandler.log(e) (at least in webkit). What I'd love to do is have the Exception class get information as to (at the very least) what called it and also, although perhaps less viable, the line number it was called on. I don't think the line number is going to work though. I would _like_ this to happen automatically, but if I have to include another argument called_from it won't be the end of the world. I know about arguments.caller but this is depreciated (as of ECMAScript 1.3 (?)).Code:/** * ExceptionHandler.js * * @object ExceptionHandler * Handles exceptions generated by the UIKit. * */ var ExceptionHandler = { /** * handle method. * Handles an exception. * * @static * @param Exception e. The exception to be handled. * @return void */ handle: function(e) { //Send Notification to alert the user NotificationCentre.sendNotification("exception", {title: e.name, message: e.message}); //Log to the console this.log(e); }, /** * log method. * Logs an exception to the console. * * @static * @param Exception e. The exception to be handled. * @return void */ log: function(e) { if($chk(console)) { console.error(e.name + ": \"" + e.message + "\""); try { console.log("Trace:"); console.trace(); console.log("[end]"); } catch(e) { console.warn("Stack trace unavailable."); } } } };
Any nice solutions/discussion would be much appreciated because I think this is an area in which javascript is lacking a little.


Reply With Quote

Bookmarks