Skip to content

Commit 5412e22

Browse files
committed
Replace stack in error only when it exists
Property `Error.stack` does not exist in some phantomjs versions. This commit makes it's use conditional.
1 parent 6b1b4eb commit 5412e22

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/v1/result.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Result {
4545
* @param {ConnectionHolder} connectionHolder - to be notified when result is either fully consumed or error happened.
4646
*/
4747
constructor(streamObserver, statement, parameters, metaSupplier, connectionHolder) {
48-
this._stack = (new Error('')).stack.substr(6); // we don't need the 'Error\n' part
48+
this._stack = captureStacktrace();
4949
this._streamObserver = streamObserver;
5050
this._p = null;
5151
this._statement = statement;
@@ -138,9 +138,7 @@ class Result {
138138
// notify connection holder that the used connection is not needed any more because error happened
139139
// and result can't bee consumed any further; call the original onError callback after that
140140
self._connectionHolder.releaseConnection().then(() => {
141-
// Error.prototype.toString() concatenates error.name and error.message nicely
142-
// then we add the rest of the stack trace
143-
error.stack = error.toString() + '\n' + this._stack;
141+
replaceStacktrace(error, this._stack);
144142
onErrorOriginal.call(observer, error);
145143
});
146144
};
@@ -150,4 +148,20 @@ class Result {
150148
}
151149
}
152150

153-
export default Result
151+
function captureStacktrace() {
152+
const error = new Error('');
153+
if (error.stack) {
154+
return error.stack.substr(6); // we don't need the 'Error\n' part
155+
}
156+
return null;
157+
}
158+
159+
function replaceStacktrace(error, newStack) {
160+
if (newStack) {
161+
// Error.prototype.toString() concatenates error.name and error.message nicely
162+
// then we add the rest of the stack trace
163+
error.stack = error.toString() + '\n' + newStack;
164+
}
165+
}
166+
167+
export default Result;

test/v1/result.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ describe('result stream', () => {
7474
});
7575

7676
it('should have a stack trace that contains code outside the driver calls', done => {
77+
if (!new Error('').stack) {
78+
done();
79+
return;
80+
}
81+
7782
// Given
7883
const fn_a = cb => fn_b(cb);
7984
const fn_b = cb => fn_c(cb);

0 commit comments

Comments
 (0)