/    Sign up×
Community /Pin to ProfileBookmark

Slightly advanced exception handling

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:

[code]
/**
* 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();
}
}
});
[/code]

and an ExceptionHandler class

[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.”);
}
}
}
};
[/code]

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 (?)).

Any nice solutions/discussion would be much appreciated because I think this is an area in which javascript is lacking a little.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 16.2010 — Maybe the reason for you did not get any answer is that the beginners do not understand the code while the experts do not like frameworks. ?
Copy linkTweet thisAlerts:
@blue-eye-labsauthorAug 16.2010 — @Kor:

on the first point: I don't really expect any beginners, on the second: I'm not sure that this is true because there are plenty of "experts" writing some really fairly impressive things using frameworks.

The framework is, however, irrelevant. All I am really looking for is a way to get a proper trace or at least a function/line reference preferably without passing a reference manually.
Copy linkTweet thisAlerts:
@KorAug 16.2010 — You may use [B][I]Function[/I].caller[/B], instead of arguments.caller (which is deprecated, indeed)
Copy linkTweet thisAlerts:
@blue-eye-labsauthorAug 16.2010 — Okay, thanks.

After some testing it looks like JS is a bit limited in this respect because of the lack of a proper OO model.

It looks like I'm going to just pass the class and method names in as a parameter.
×

Success!

Help @blue-eye-labs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...