Created
June 12, 2015 20:19
-
-
Save dmajda/fe2ed00ba59bd9ebd278 to your computer and use it in GitHub Desktop.
Tests for code that adds the stack property to an Error subclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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