Skip to content
Open
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
7 changes: 6 additions & 1 deletion lib/annotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ exports.error = function (stripColorCodes) {

internals.safeStringify = function (obj, spaces) {

return JSON.stringify(obj, internals.serializer(), spaces);
try {
return JSON.stringify(obj, internals.serializer(), spaces);
}
catch {
return '[object could not be serialized]';
}
};


Expand Down
21 changes: 21 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,27 @@ describe('errors', () => {
expect(() => err.annotate(true)).to.not.throw();
});

it('handles objects with toJSON that throws', () => {

const schema = Joi.object({
type: Joi.string().required()
}).unknown(false);

const badObj = {};
Object.defineProperty(badObj, 'toJSON', {
value: function () {

throw new TypeError('Receiver must be an instance');
},
enumerable: false
});

const err = schema.validate({ someUrl: badObj }).error;
expect(err).to.be.an.error();
expect(() => err.annotate()).to.not.throw();
expect(err.annotate(true)).to.contain('"type" is required');
});

it('annotates joi schema error', () => {

const schema = Joi.object({
Expand Down