From cf64e549a2b28aea91062dea67db8733b4ecdd6f Mon Sep 17 00:00:00 2001 From: Jonas L Date: Tue, 11 Jul 2023 14:23:30 +0200 Subject: [PATCH] feat: add details to raise exceptions (#240) --- hcloud/_exceptions.py | 4 +-- hcloud/actions/domain.py | 10 +++++-- tests/unit/actions/test_domain.py | 49 ++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/hcloud/_exceptions.py b/hcloud/_exceptions.py index 36fee5de..d0801e95 100644 --- a/hcloud/_exceptions.py +++ b/hcloud/_exceptions.py @@ -6,9 +6,7 @@ class APIException(HCloudException): """There was an error while performing an API Request""" def __init__(self, code, message, details): + super().__init__(message) self.code = code self.message = message self.details = details - - def __str__(self): - return self.message diff --git a/hcloud/actions/domain.py b/hcloud/actions/domain.py index 0d91eaca..1cc25d47 100644 --- a/hcloud/actions/domain.py +++ b/hcloud/actions/domain.py @@ -61,12 +61,18 @@ class ActionException(HCloudException): """A generic action exception""" def __init__(self, action): + message = self.__doc__ + if action.error is not None and "message" in action.error: + message += f": {action.error['message']}" + + super().__init__(message) + self.message = message self.action = action class ActionFailedException(ActionException): - """The Action you were waiting for failed""" + """The pending action failed""" class ActionTimeoutException(ActionException): - """The Action you were waiting for timed out""" + """The pending action timed out""" diff --git a/tests/unit/actions/test_domain.py b/tests/unit/actions/test_domain.py index 02959da7..54b730c7 100644 --- a/tests/unit/actions/test_domain.py +++ b/tests/unit/actions/test_domain.py @@ -1,7 +1,14 @@ import datetime from datetime import timezone -from hcloud.actions.domain import Action +import pytest + +from hcloud.actions.domain import ( + Action, + ActionException, + ActionFailedException, + ActionTimeoutException, +) class TestAction: @@ -15,3 +22,43 @@ def test_started_finished_is_datetime(self): assert action.finished == datetime.datetime( 2016, 3, 30, 23, 50, tzinfo=timezone.utc ) + + +def test_action_exceptions(): + with pytest.raises( + ActionException, + match=r"The pending action failed: Server does not exist anymore", + ): + raise ActionFailedException( + action=Action( + **{ + "id": 1084730887, + "command": "change_server_type", + "status": "error", + "progress": 100, + "resources": [{"id": 34574042, "type": "server"}], + "error": { + "code": "server_does_not_exist_anymore", + "message": "Server does not exist anymore", + }, + "started": "2023-07-06T14:52:42+00:00", + "finished": "2023-07-06T14:53:08+00:00", + } + ) + ) + + with pytest.raises(ActionException, match=r"The pending action timed out"): + raise ActionTimeoutException( + action=Action( + **{ + "id": 1084659545, + "command": "create_server", + "status": "running", + "progress": 50, + "started": "2023-07-06T13:58:38+00:00", + "finished": None, + "resources": [{"id": 34572291, "type": "server"}], + "error": None, + } + ) + )