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

fix(shell-api): improve invalid input errors MONGOSH-600 #742

Merged
merged 2 commits into from Mar 26, 2021

Conversation

rose-m
Copy link
Contributor

@rose-m rose-m commented Mar 24, 2021

Adds the affected method name to the InvalidInputErrors of the Shell API.

@rose-m rose-m self-assigned this Mar 24, 2021
Copy link
Contributor

@gribnoysup gribnoysup left a comment

Choose a reason for hiding this comment

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

Overall looks good to me! Definitely improves the experience of using shell. I noticed that there is ReplicaSet.add, and global connect and load methods in shell-api that are still using assertArgsDefined, should we also switch them to your new validation method?

If you don't mind, I think I will also open a ticket for the future to enhance this with some automated code generation as we discussed yesterday. I think this would be better for us in the long run if such validation is added to shell-api public interface (semi-)automatically

EDIT: Oh, also wanted to say that I haven't noticed any new tests added and I thought that maybe we can add just a few that check the error message is generated correctly, what do you think?

@@ -234,7 +233,7 @@ export default class Database extends ShellApiClass {
*/
@returnsPromise
async runCommand(cmd: Document): Promise<Document> {
assertArgsDefined(cmd);
assertArgsDefinedType([cmd], [true], 'Database.runCommand');
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if we want to do something about it in the scope of this ticket, but we don't have any validation if input is a document and this leads to some very weird results when I'm providing non document input:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah for these cases I did not start to change even more but I tried to just "replace" where I could immediately infer the logic. But in these cases it would probably make sense to check for typeof cmd === 'object'.

@rose-m
Copy link
Contributor Author

rose-m commented Mar 24, 2021

Huh I didn't see the other usages then... I'll cover those, too. And yep, sounds good to add a unit test for the assertArgsDefinedType and the logic there.

And yes, absolutely - ticket for the auto-generation would be awesome!!

@rose-m rose-m requested a review from gribnoysup March 25, 2021 11:43
Copy link
Contributor

@gribnoysup gribnoysup left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines +26 to +72
describe('assertArgsDefinedType', () => {
it('allows to specify an argument must be defined', () => {
try {
assertArgsDefinedType([1, undefined], [true, true], 'helper.test');
} catch (e) {
expect(e.message).to.contain('Missing required argument at position 1');
expect(e.message).to.contain('helper.test');
return;
}
expect.fail('Expected error');
});
it('allows to specify a single argument type', () => {
[null, 2, {}].forEach(value => {
try {
assertArgsDefinedType([1, value], [true, 'string'], 'helper.test');
} catch (e) {
expect(e.message).to.contain('Argument at position 1 must be of type string');
expect(e.message).to.contain('helper.test');
return;
}
expect.fail('Expected error');
});
expect(() => assertArgsDefinedType([1, 'test'], [true, 'string'])).to.not.throw;
});
it('allows to specify multiple argument types', () => {
[null, {}].forEach(value => {
try {
assertArgsDefinedType([1, value], [true, ['number', 'string']]);
} catch (e) {
return expect(e.message).to.contain('Argument at position 1 must be of type number or string');
}
expect.fail('Expected error');
});
expect(() => assertArgsDefinedType([1, 'test'], [true, ['number', 'string']])).to.not.throw;
expect(() => assertArgsDefinedType([1, 2], [true, ['number', 'string']])).to.not.throw;
});
it('allows to specify an optional argument with type', () => {
expect(() => assertArgsDefinedType([1, undefined], [true, [undefined, 'string']])).to.not.throw;
expect(() => assertArgsDefinedType([1, 'test'], [true, [undefined, 'string']])).to.not.throw;
try {
assertArgsDefinedType([1, 2], [true, [undefined, 'string']]);
} catch (e) {
return expect(e.message).to.contain('Argument at position 1 must be of type string');
}
expect.fail('Expected error');
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding those, so easy to assert how this new method works at a glance now!

@rose-m rose-m force-pushed the MONGOSH-600-improve-shell-api-invalid-input-errors branch from 673b1f7 to a36b37d Compare March 25, 2021 12:33
@rose-m rose-m merged commit abc0317 into master Mar 26, 2021
@rose-m rose-m deleted the MONGOSH-600-improve-shell-api-invalid-input-errors branch March 26, 2021 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants