Skip to content

Commit 035f675

Browse files
authored
Merge pull request #303 from legraphista/feature/1.5/stack-trace-outside-the-driver
Errors from Result will now contain the caller stack trace
2 parents 7dc7ea3 + 04f1248 commit 035f675

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/v1/result.js

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +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
4849
this._streamObserver = streamObserver;
4950
this._p = null;
5051
this._statement = statement;
@@ -137,6 +138,9 @@ class Result {
137138
// notify connection holder that the used connection is not needed any more because error happened
138139
// and result can't bee consumed any further; call the original onError callback after that
139140
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;
140144
onErrorOriginal.call(observer, error);
141145
});
142146
};

test/v1/result.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,27 @@ describe('result stream', () => {
7272
}
7373
});
7474
});
75+
76+
it('should have a stack trace that contains code outside the driver calls', done => {
77+
// Given
78+
const fn_a = cb => fn_b(cb);
79+
const fn_b = cb => fn_c(cb);
80+
const fn_c = cb => session.run('RETURN 1/0 AS x').catch(cb);
81+
82+
// When
83+
fn_a(err => {
84+
const stack = err.stack;
85+
86+
// Then
87+
const contains_fn_a = /at fn_a \(.*?\/result.test.js:\d+:\d+\)/.test(stack);
88+
const contains_fn_b = /at fn_b \(.*?\/result.test.js:\d+:\d+\)/.test(stack);
89+
const contains_fn_c = /at fn_c \(.*?\/result.test.js:\d+:\d+\)/.test(stack);
90+
91+
expect(contains_fn_a).toBeTruthy();
92+
expect(contains_fn_b).toBeTruthy();
93+
expect(contains_fn_c).toBeTruthy();
94+
95+
done();
96+
});
97+
});
7598
});

0 commit comments

Comments
 (0)