From e0e72209f085334819000467d1dcdcaaa5570c17 Mon Sep 17 00:00:00 2001 From: Andreas Lind Petersen Date: Sun, 31 Mar 2013 18:30:28 +0200 Subject: [PATCH] Declare JS_Parse_Error and DefaultsError the same way, make both instanceof Error, and also capture a stack trace in the DefaultsError constructor. --- lib/parse.js | 9 ++++----- lib/utils.js | 8 ++++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index da39b5b1..22c0aa8d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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); diff --git a/lib/utils.js b/lib/utils.js index c95b9824..42bd5ffa 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 = {};