Resolve incorrect error message when creating duplicate item without required field.#177
Conversation
1 similar comment
|
Nice job nailing down the source of this error! I see exactly what you're getting to, although I can't convince myself that the normal error for multiple results will still work as intended. We want that error "Error: Missing required fields: inventory" to be thrown, and I think your test is good for this. Here's what I would offer as a small adjustment to what you did: Take these lines: Move them to before the call to With that, you can integrate this error message into your except block: Depending on if There are other ways to do this too, and I could be wrong about the existing multiple results scenario too. |
|
@AlanCoding Nice catch! Never a good idea to mute an exception which is meant to be thrown. After studying I decided to restructuring rather than simply adding exception catch. In my opinion, the Now the outcome becomes: And Note some unit tests were also modified. |
lib/tower_cli/models/base.py
Outdated
| required_fields = [i.key or i.name | ||
| for i in self.fields if i.required] | ||
| missing_fields = [i for i in required_fields if i not in kwargs] | ||
| if missing_fields and not pk: |
There was a problem hiding this comment.
Part of the problem here is that pk could be defined after the subsequent to _lookup is done, and that's part of what was originally being tested for (I think, and I think that's tied up with the test changes you were forced to make). I would have the missing_fields calc happen even before the if not pk: block, and then maybe use a conditional along with fail_on_found to raise another type of error of that happens (this could still be in the original except block you used). FOF is a good indicator that this is called by the create command.
|
It turns out that the very desired exception is not able to be raised in this scenario because the logic is locked: one can only run the lookup to determine whether to create or modify resource, yet the required field constraint is only valid when creating resource. A walk-around is to make the error message more informative, so that users can more easily spot the cause of error. |
|
👍 This looks fine. Let's go ahead and merge this and close the ticket out. |
Connect to issue #109.
This issue is caused by the exe sequence of existence checking and required checking. Adding related exception capture gives user log info of possible failure causes.
Now the outcome becomes: