-
Notifications
You must be signed in to change notification settings - Fork 398
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
Python 3 compatibility error handling: use to_native(e) instead of str(e) or e.me… #26
Conversation
As an extra note, this PR is not much more than a search and replace. I have not thoroughly tested this, and have (so far) only used the elasticache module in it. I will be gradually using the other elements as much as possible, favoring this repository over the modules shipped with ansible 2.9.5 . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the time to submit this patch. I apologise for the delay in reviewing.
The vast bulk of these changes look to be handing failure modes. The long term fix is to migrate to AnsibleAWSModule and then start using fail_json_aws.
fail_json_aws(e, msg="Some description of what you were doing")
However, to avoid perfect being the enemy of good I'd suggest using it only in the module where we're already using AnsibleAWSModule (see the only module where I explicitly suggest it)
We also have a helper that lets us do "except is_boto3_error('SomeBotoError'):" rather than nested if statements. I prefer these as you're less likely to accidentally drop an Exception on the floor.
plugins/modules/ec2_vpc_endpoint.py
Outdated
@@ -296,18 +297,19 @@ def create_vpc_endpoint(client, module): | |||
if not status_achieved: | |||
module.fail_json(msg='Error waiting for vpc endpoint to become available - please check the AWS console') | |||
except botocore.exceptions.ClientError as e: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a helper function (is_boto3_error_code) which I think would be better here:
from ansible_collections.amazon.aws.plugins.module_utils.aws.core import is_boto3_error_code
except is_boto3_error_code('DryRunOperation'):
changed = True
result = 'Would have created VPC Endpoint if not in check mode'
except is_boto3_error_core('IdempotentParameterMismatch'):
...
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError as e):
module.fail_json(msg=to_native(e), exception=traceback.format_exc(),
This avoids an additional level of nesting.
@@ -277,7 +277,7 @@ def register_task(self, family, task_role_arn, execution_role_arn, network_mode, | |||
try: | |||
response = self.ecs.register_task_definition(**params) | |||
except botocore.exceptions.ClientError as e: | |||
self.module.fail_json(msg=e.message, **camel_dict_to_snake_dict(e.response)) | |||
self.module.fail_json(msg=to_native(e), **camel_dict_to_snake_dict(e.response)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This module is using AnsibleAWSModule, which means we have a 'fail' method which is much easier to use and avoids the need to for the extra import.
self.module.fail_json(msg=to_native(e), **camel_dict_to_snake_dict(e.response)) | |
self.module.fail_json_aws(e, msg="Failed to register task") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ansible_collections.amazon.aws.plugins.module_utils.aws.core
got renamed to ansible_collections.amazon.aws.plugins.module_utils.core
Normally we'd have put a shim in place, but because we were prior to the first release of the collection only main was updated. I'm sorry about this. If you can get this fixed up I'll try following up and see if we can get this cleanup merged.
@tremble I fixed it up. I grepped the rest just in case to find other refs to Based on how long it took for me to respond to the feedback and requested changes, I don't think I would be in any position to complain about having to come back and fix a few imports! 😅 |
I have attempted to merge, but the increasing scope of the changes is making me slightly nervous, as the scope is increasing. A lot of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think using fail_json_aws definitely falls into the 'compatibility' bucket, but I can see why you're worried about scope where things have migrated over to is_boto3_error_code.
That said, I very much appreciate you being willing to follow up with this, having just mass-updated everything to AnsibleAWSModule, I would likely have been following up with some of this cleanup myself in the near future.
I pushed a missing import and a couple of comments to tell the linter to ignore warnings it was throwing, but I think we're good to merge this I just need poke the CI due to some known test flakes.
…tead of str(e) or e.me… (ansible-collections#26) * Py3 compat error handling: use to_native(e) instead of str(e) or e.message * PR comment changes, use fail_json_aws and is_boto3_error_code This commit was initially merged in https://github.com/ansible-collections/community.aws See: ansible-collections@ffe14f9
…r(e) or e.me… (ansible-collections#26) * Py3 compat error handling: use to_native(e) instead of str(e) or e.message * PR comment changes, use fail_json_aws and is_boto3_error_code
…r(e) or e.me… (ansible-collections#26) * Py3 compat error handling: use to_native(e) instead of str(e) or e.message * PR comment changes, use fail_json_aws and is_boto3_error_code
* Move module_utils out of subdir and update imports * Use relative imports * Always get dict transformations from their canonical home * Split imports by line to match tremble's other refactoring * Make import order more consistent * Temporarily restore legacy module_utils files
…r(e) or e.me… (ansible-collections#26) * Py3 compat error handling: use to_native(e) instead of str(e) or e.message * PR comment changes, use fail_json_aws and is_boto3_error_code
…r(e) or e.me… (ansible-collections#26) * Py3 compat error handling: use to_native(e) instead of str(e) or e.message * PR comment changes, use fail_json_aws and is_boto3_error_code This commit was initially merged in https://github.com/ansible-collections/community.aws See: ansible-collections@ffe14f9
…r(e) or e.me… (ansible-collections#26) * Py3 compat error handling: use to_native(e) instead of str(e) or e.message * PR comment changes, use fail_json_aws and is_boto3_error_code This commit was initially merged in https://github.com/ansible-collections/community.aws See: ansible-collections@ffe14f9
…r(e) or e.me… (ansible-collections#26) * Py3 compat error handling: use to_native(e) instead of str(e) or e.message * PR comment changes, use fail_json_aws and is_boto3_error_code This commit was initially merged in https://github.com/ansible-collections/community.aws See: ansible-collections@ffe14f9
…r(e) or e.me… (ansible-collections#26) * Py3 compat error handling: use to_native(e) instead of str(e) or e.message * PR comment changes, use fail_json_aws and is_boto3_error_code This commit was initially merged in https://github.com/ansible-collections/community.aws See: ansible-collections@ffe14f9
SUMMARY
This simply replaces the use of
e.message
andstr(e)
on exceptions to use ansible'sto_native
I had to patch elasticache as it was failing for me, and patched everything while at it.
ISSUE TYPE
COMPONENT NAME
A bunch of different components, same fix and error handling almost everywhere.
ADDITIONAL INFORMATION
This is the original issue I had that prompted the fix.
With this change it works.