Details
-
Bug
-
Resolution: Fixed
-
P3
-
8
-
None
-
b112
-
generic
-
generic
Description
Calling captureStackTrace has the side effect to call toString on the error instance. If called during initialization we end calling toString on an instance that is not fully initialised.
This issue impacts (at least) mongoose node module document validation.
From v8 StackTrace API:
Error.captureStackTrace(error, constructorOpt)
adds a stack property to the given error object that will yield the stack trace at the time captureStackTrace was called. The reason for not just returning the formatted stack trace directly is that this way we can postpone the formatting of the stack trace until the stack property is accessed and avoid formatting completely if it never is.
Test case:
function MyError () {
Error.call(this);
// Side effect calls toString on non fully initialized instance.
Error.captureStackTrace(this);
this.arr = {};
};
MyError.prototype.toString = function() {
Object.keys(this.arr).forEach(function (key) {
}, this)
}
var e = new MyError();
This issue impacts (at least) mongoose node module document validation.
From v8 StackTrace API:
Error.captureStackTrace(error, constructorOpt)
adds a stack property to the given error object that will yield the stack trace at the time captureStackTrace was called. The reason for not just returning the formatted stack trace directly is that this way we can postpone the formatting of the stack trace until the stack property is accessed and avoid formatting completely if it never is.
Test case:
function MyError () {
Error.call(this);
// Side effect calls toString on non fully initialized instance.
Error.captureStackTrace(this);
this.arr = {};
};
MyError.prototype.toString = function() {
Object.keys(this.arr).forEach(function (key) {
}, this)
}
var e = new MyError();