Skip to content

Commit 5228d5e

Browse files
committed
Merge pull request #58 from cjihrig/stacks
handle errors without proper stacks
2 parents 80fb6c3 + ebee954 commit 5228d5e

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lib/index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ internals.Assertion.prototype.assert = function (result, verb, actual, expected)
8080
this._ref instanceof Error) {
8181

8282
const original = this._ref;
83-
original.at = internals.at(this._ref);
83+
const at = internals.at(this._ref);
84+
85+
if (at !== undefined) {
86+
original.at = at;
87+
}
8488

8589
throw original;
8690
}
@@ -432,12 +436,14 @@ internals.type = function (value) {
432436
internals.at = function (error) {
433437

434438
error = error || new Error();
435-
const at = error.stack.replace(error.toString(), '').split('\n').slice(1).filter(internals.filterLocal)[0].match(/^\s*at \(?(.+)\:(\d+)\:(\d+)\)?$/);
436-
return {
439+
const stack = typeof error.stack === 'string' ? error.stack : '';
440+
const frame = stack.replace(error.toString(), '').split('\n').slice(1).filter(internals.filterLocal)[0] || '';
441+
const at = frame.match(/^\s*at \(?(.+)\:(\d+)\:(\d+)\)?$/);
442+
return Array.isArray(at) ? {
437443
filename: at[1],
438444
line: at[2],
439445
column: at[3]
440-
};
446+
} : undefined;
441447
};
442448

443449

test/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,23 @@ describe('expect()', () => {
10391039
Hoek.assert(exception.message === 'some message', exception);
10401040
done();
10411041
});
1042+
1043+
it('validates assertion (error with incomplete stack)', (done) => {
1044+
1045+
let exception = false;
1046+
try {
1047+
const err = new Error('foo');
1048+
err.stack = undefined;
1049+
Code.expect(err).to.not.exist();
1050+
}
1051+
catch (err) {
1052+
exception = err;
1053+
}
1054+
1055+
Hoek.assert(exception.message === 'foo', exception);
1056+
Hoek.assert(exception.at === undefined, exception);
1057+
done();
1058+
});
10421059
});
10431060

10441061
describe('empty()', () => {

0 commit comments

Comments
 (0)