Johan
Johan

Reputation: 35194

Inspecting custom exceptions

I'm trying to create a custom exception with some useful debug information:

var customError = function(name, message) {

    this.message = message;
    this.name = name;

    this.prototype = new Error(); // to make customError "inherit" from the 
                                  // default error
};

throw new customError('CustomException', 'Something went wrong'); 

But all I'm getting are fuzzy messages in the console:

IE: "SCRIPT5022: Exception thrown and not caught "

Firefox: "uncaught exception: [object Object]"

What do I need to change in order to get something useful out of my exceptions?

Upvotes: 0

Views: 89

Answers (1)

Zeta
Zeta

Reputation: 105886

Wrong usage of prototype:

var customError = function(name, message) {    
    this.message = message;
    this.name = name;
};

customError.prototype = new Error(); // to make customError "inherit" from the 
                                     // default error

throw new customError('CustomException', 'Something went wrong'); 

prototype is a property of the constructor (customError), not of the constructed object (this), see ECMA sec 4.2.1 and 4.3.5.

Upvotes: 1

Related Questions