Declare JS_Parse_Error and DefaultsError the same way, make both instanceof Error, and also capture a stack trace in the DefaultsError constructor.

This commit is contained in:
Andreas Lind Petersen 2013-03-31 18:30:28 +02:00
parent 2b8e206fec
commit e0e72209f0
2 changed files with 10 additions and 7 deletions

View File

@ -178,16 +178,15 @@ function parse_js_number(num) {
};
function JS_Parse_Error(message, line, col, pos) {
this.message = message;
this.message = message + " (line: " + line + ", col: " + col + ", pos: " + pos + ")";
this.line = line;
this.col = col;
this.pos = pos;
this.stack = new Error().stack;
// The second parameter omits the JS_Parse_Error constructor from the captured stack trace:
Error.captureStackTrace(this, JS_Parse_Error);
};
JS_Parse_Error.prototype.toString = function() {
return this.message + " (line: " + this.line + ", col: " + this.col + ", pos: " + this.pos + ")" + "\n\n" + this.stack;
};
JS_Parse_Error.prototype = Error.prototype;
function js_error(message, filename, line, col, pos) {
throw new JS_Parse_Error(message, line, col, pos);

View File

@ -81,11 +81,15 @@ function repeat_string(str, i) {
return d;
};
function DefaultsError(msg, defs) {
this.msg = msg;
function DefaultsError(message, defs) {
this.message = message + "(defs: " + JSON.stringify(defs) + ")";
this.defs = defs;
// The second parameter omits the DefaultError constructor from the captured stack trace:
Error.captureStackTrace(this, DefaultsError);
};
DefaultsError.prototype = Error.prototype;
function defaults(args, defs, croak) {
if (args === true)
args = {};