Skip to content

Instantly share code, notes, and snippets.

@dmajda
Created June 12, 2015 20:19
Show Gist options
  • Save dmajda/fe2ed00ba59bd9ebd278 to your computer and use it in GitHub Desktop.
Save dmajda/fe2ed00ba59bd9ebd278 to your computer and use it in GitHub Desktop.
Tests for code that adds the stack property to an Error subclass
/* See https://github.com/pegjs/pegjs/pull/342 */
function subclass(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor();
}
function StackError() {
if (typeof Error.captureStackTrace !== "function") {
err = new Error();
if (typeof Object.defineProperty === "function") {
Object.defineProperty(this, "stack", {
get: function () {
return err.stack;
}
});
} else {
this.stack = err.stack;
}
} else {
Error.captureStackTrace(this, StackError);
}
}
function NoStackError() {
}
subclass(StackError, Error);
subclass(NoStackError, Error);
// throw new Error();
// throw new NoStackError();
// throw new StackError();
// try { throw new Error() } catch (e) { console.log(e.stack); }
// try { throw new NoStackError() } catch (e) { console.log(e.stack); }
// try { throw new StackError() } catch (e) { console.log(e.stack); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment