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

feat: improve hcloud library exceptions handling #243

Merged
merged 1 commit into from
Jul 20, 2023

Conversation

jooola
Copy link
Collaborator

@jooola jooola commented Jun 27, 2023

SUMMARY

Related to #220

Properly handle the different type of exceptions raised by the hcloud library.

Improve the stack traces printed to the users.

ISSUE TYPE
  • Feature Pull Request

@jooola jooola force-pushed the exceptions branch 4 times, most recently from d081bdc to ab73bd6 Compare June 27, 2023 14:59
@jooola jooola added this to the v2.0.0 milestone Jul 5, 2023
@jooola jooola force-pushed the exceptions branch 3 times, most recently from e0cb009 to 34b7850 Compare July 5, 2023 14:46
@jooola
Copy link
Collaborator Author

jooola commented Jul 5, 2023

This is based onto #244, sorry for the messy commit history.

Now that we handle the exception in a single place, we can collect some extra data and provide it to the users in the returned data.

Here is an example of failing task when running with a verbose mode, we see the extra details in the hcloud dict:

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_hcloud_floating_ip_payload_0xy5_gov/ansible_hcloud_floating_ip_payload.zip/ansible_collections/hetzner/hcloud/plugins/modules/hcloud_floating_ip.py", line 221, in _create_floating_ip
  File "/tmp/ansible_hcloud_floating_ip_payload_0xy5_gov/ansible_hcloud_floating_ip_payload.zip/ansible_collections/hetzner/hcloud/plugins/module_utils/vendor/hcloud/floating_ips/client.py", line 296, in create
    response = self._client.request(url="/floating_ips", json=data, method="POST")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/ansible_hcloud_floating_ip_payload_0xy5_gov/ansible_hcloud_floating_ip_payload.zip/ansible_collections/hetzner/hcloud/plugins/module_utils/vendor/hcloud/hcloud.py", line 221, in request
    self._raise_exception_from_content(content)
  File "/tmp/ansible_hcloud_floating_ip_payload_0xy5_gov/ansible_hcloud_floating_ip_payload.zip/ansible_collections/hetzner/hcloud/plugins/module_utils/vendor/hcloud/hcloud.py", line 180, in _raise_exception_from_content
    raise APIException(
ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud._exceptions.APIException: invalid input in fields 'server', 'home_location'
fatal: [testhost]: FAILED! => {
    "changed": false,
    "hcloud": {
        "code": "invalid_input",
        "details": {
            "fields": [
                {
                    "messages": [
                        "either server or home_location must be provided"
                    ],
                    "name": "server"
                },
                {
                    "messages": [
                        "either server or home_location must be provided"
                    ],
                    "name": "home_location"
                }
            ]
        },
        "message": "invalid input in fields 'server', 'home_location'"
    },
    "invocation": {
        "module_args": {
            "api_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "delete_protection": null,
            "description": null,
            "endpoint": "https://api.hetzner.cloud/v1",
            "force": null,
            "home_location": "abc",
            "id": null,
            "labels": null,
            "name": "ansible-test-82485377-jomob-i",
            "server": null,
            "state": "present",
            "type": "ipv4"
        }
    },
    "msg": "invalid input in fields 'server', 'home_location'"
}

If the verbose mode is not enabled, we get the following:

TASK [hcloud_floating_ip : test delete Floating IP fails if it is protected] ***
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible_collections.hetzner.hcloud.plugins.module_utils.vendor.hcloud._exceptions.APIException: Floating IP deletion is protected
fatal: [testhost]: FAILED! => {"changed": false, "hcloud": {"code": "protected", "details": null, "message": "Floating IP deletion is protected"}, "msg": "Floating IP deletion is protected"}

I haven't figured how to clearly detail a failed action, for now I return the entire action object (we might need to convert it to a dict though).

@jooola jooola force-pushed the exceptions branch 3 times, most recently from aa2ee69 to 3fa6e46 Compare July 11, 2023 09:34
@jooola jooola force-pushed the exceptions branch 2 times, most recently from 7609403 to 8deca37 Compare July 19, 2023 13:32
@jooola jooola marked this pull request as ready for review July 20, 2023 07:08
@apricote apricote self-requested a review July 20, 2023 07:18
@jooola jooola merged commit c15377c into ansible-collections:main Jul 20, 2023
17 of 19 checks passed
@jooola jooola deleted the exceptions branch July 20, 2023 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants