Skip to content

Commit

Permalink
feat: add details to raise exceptions (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jul 11, 2023
1 parent 443bf26 commit cf64e54
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
4 changes: 1 addition & 3 deletions hcloud/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 8 additions & 2 deletions hcloud/actions/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
49 changes: 48 additions & 1 deletion tests/unit/actions/test_domain.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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,
}
)
)

0 comments on commit cf64e54

Please sign in to comment.