Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ internals.Assertion.prototype.assert = function (result, verb, actual, expected)
this._ref instanceof Error) {

const original = this._ref;
original.at = internals.at(this._ref);
const at = internals.at(this._ref);

if (at !== undefined) {
original.at = at;
}

throw original;
}
Expand Down Expand Up @@ -432,12 +436,14 @@ internals.type = function (value) {
internals.at = function (error) {

error = error || new Error();
const at = error.stack.replace(error.toString(), '').split('\n').slice(1).filter(internals.filterLocal)[0].match(/^\s*at \(?(.+)\:(\d+)\:(\d+)\)?$/);
return {
const stack = typeof error.stack === 'string' ? error.stack : '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be

const stack = error.stack || '';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I'd rather ensure that we have a string though. Someone could create a new Error object and set the stack to something else. This will help keep code from breaking down if they do something stupid.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

const frame = stack.replace(error.toString(), '').split('\n').slice(1).filter(internals.filterLocal)[0] || '';
const at = frame.match(/^\s*at \(?(.+)\:(\d+)\:(\d+)\)?$/);
return Array.isArray(at) ? {
filename: at[1],
line: at[2],
column: at[3]
};
} : undefined;
};


Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,23 @@ describe('expect()', () => {
Hoek.assert(exception.message === 'some message', exception);
done();
});

it('validates assertion (error with incomplete stack)', (done) => {

let exception = false;
try {
const err = new Error('foo');
err.stack = undefined;
Code.expect(err).to.not.exist();
}
catch (err) {
exception = err;
}

Hoek.assert(exception.message === 'foo', exception);
Hoek.assert(exception.at === undefined, exception);
done();
});
});

describe('empty()', () => {
Expand Down