Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw config error based on the joi validation error #29137

Merged
merged 1 commit into from
Jan 23, 2019
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
5 changes: 4 additions & 1 deletion src/server/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ export class Config {
});

if (results.error) {
throw results.error;
const error = new Error(results.error.message);
Copy link
Member

Choose a reason for hiding this comment

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

note: I prefer your solution to something like delete results.error._object; or dealing with any.error(...) since:

  • we don't rely on Joi internals
  • we throw error that we control (and ideally we should do that everywhere)

error.name = results.error.name;
error.stack = results.error.stack;
throw error;
}

this[vals] = results.value;
Expand Down
8 changes: 6 additions & 2 deletions src/server/config/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('lib/config/config', function () {
});

it('should thow an exception when setting a value with the wrong type', function (done) {
expect.assertions(2);
expect.assertions(4);

const run = function () {
config.set('test.enable', 'something');
Expand All @@ -184,7 +184,11 @@ describe('lib/config/config', function () {
run();
} catch (err) {
expect(err).toHaveProperty('name', 'ValidationError');
expect(err.details[0].message).toBe('"enable" must be a boolean');
expect(err).toHaveProperty('message',
'child \"test\" fails because [child \"enable\" fails because [\"enable\" must be a boolean]]'
);
expect(err).not.toHaveProperty('details');
expect(err).not.toHaveProperty('_object');
}

done();
Expand Down