Skip to content

Commit cf64e54

Browse files
authored
feat: add details to raise exceptions (#240)
1 parent 443bf26 commit cf64e54

3 files changed

Lines changed: 57 additions & 6 deletions

File tree

hcloud/_exceptions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ class APIException(HCloudException):
66
"""There was an error while performing an API Request"""
77

88
def __init__(self, code, message, details):
9+
super().__init__(message)
910
self.code = code
1011
self.message = message
1112
self.details = details
12-
13-
def __str__(self):
14-
return self.message

hcloud/actions/domain.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,18 @@ class ActionException(HCloudException):
6161
"""A generic action exception"""
6262

6363
def __init__(self, action):
64+
message = self.__doc__
65+
if action.error is not None and "message" in action.error:
66+
message += f": {action.error['message']}"
67+
68+
super().__init__(message)
69+
self.message = message
6470
self.action = action
6571

6672

6773
class ActionFailedException(ActionException):
68-
"""The Action you were waiting for failed"""
74+
"""The pending action failed"""
6975

7076

7177
class ActionTimeoutException(ActionException):
72-
"""The Action you were waiting for timed out"""
78+
"""The pending action timed out"""

tests/unit/actions/test_domain.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import datetime
22
from datetime import timezone
33

4-
from hcloud.actions.domain import Action
4+
import pytest
5+
6+
from hcloud.actions.domain import (
7+
Action,
8+
ActionException,
9+
ActionFailedException,
10+
ActionTimeoutException,
11+
)
512

613

714
class TestAction:
@@ -15,3 +22,43 @@ def test_started_finished_is_datetime(self):
1522
assert action.finished == datetime.datetime(
1623
2016, 3, 30, 23, 50, tzinfo=timezone.utc
1724
)
25+
26+
27+
def test_action_exceptions():
28+
with pytest.raises(
29+
ActionException,
30+
match=r"The pending action failed: Server does not exist anymore",
31+
):
32+
raise ActionFailedException(
33+
action=Action(
34+
**{
35+
"id": 1084730887,
36+
"command": "change_server_type",
37+
"status": "error",
38+
"progress": 100,
39+
"resources": [{"id": 34574042, "type": "server"}],
40+
"error": {
41+
"code": "server_does_not_exist_anymore",
42+
"message": "Server does not exist anymore",
43+
},
44+
"started": "2023-07-06T14:52:42+00:00",
45+
"finished": "2023-07-06T14:53:08+00:00",
46+
}
47+
)
48+
)
49+
50+
with pytest.raises(ActionException, match=r"The pending action timed out"):
51+
raise ActionTimeoutException(
52+
action=Action(
53+
**{
54+
"id": 1084659545,
55+
"command": "create_server",
56+
"status": "running",
57+
"progress": 50,
58+
"started": "2023-07-06T13:58:38+00:00",
59+
"finished": None,
60+
"resources": [{"id": 34572291, "type": "server"}],
61+
"error": None,
62+
}
63+
)
64+
)

0 commit comments

Comments
 (0)