My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

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 ;) )

3 comments:

  1. 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?

    ReplyDelete
  2. 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! )

    ReplyDelete

Note: Only a member of this blog may post a comment.