Thursday, July 01, 2010

JavaScript FatalError

just in case at some point you decide to break everything, regardless possible try catches around ...

function FatalError(message, file, line){
function toString() {
throw e;
}
var e = this;
e["@message"] = message;
e["@file"] = file || e.file || e.fileName;
e["@line"] = line || e.line || e.lineNumber;
if ("__defineGetter__" in e) {
e.__defineGetter__("message", toString);
e.__defineGetter__("description", toString);
} else {
e.message = e.description = {toString: toString};
}
// just in case, but not necessary
e.toString = e.valueOf = toString;
};
(FatalError.prototype = new Error).name = "FatalError";

how to use it?

throw new FatalError("this must be a joke!");

P.S. it won't work if Errors are not logged ( silent failures, definitively a problem for certain applications ;) )

Labels: , , , ,

3 Comments:

Blogger Corey Hart said...

When using a trying to log the error in a try/catch block, I get this error in the console:

[unsupported: no toString() function in type object]


Any ideas?

01 July, 2010 19:37  
Blogger Andrea Giammarchi said...

you just demonstrated that FatalError works :D

It's "unloggable" with normal procedures, so it's handy to spot silent failures in the middle of whatever code, or to break some trapped loop and check the behavior ... I know it sounds silly, but one day somebody will need it ( I actually did! )

01 July, 2010 19:43  
Blogger a.in.the.k said...

excelent ;-) love this one

23 July, 2010 22:35  

Post a Comment

Links to this post:

Create a Link

<< Home