Skip to content

Commit 16193c3

Browse files
committed
Add VultrException
1 parent e2172c3 commit 16193c3

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=cssnr_vultr-python&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=cssnr_vultr-python)
77
[![Workflow Lint](https://img.shields.io/github/actions/workflow/status/cssnr/vultr-python/lint.yaml?logo=cachet&label=lint)](https://github.com/cssnr/vultr-python/actions/workflows/lint.yaml)
88
[![Workflow Test](https://img.shields.io/github/actions/workflow/status/cssnr/vultr-python/test.yaml?logo=cachet&label=test)](https://github.com/cssnr/vultr-python/actions/workflows/test.yaml)
9-
[![Deployments PyPi](https://img.shields.io/github/deployments/cssnr/vultr-python/pypi?logo=materialformkdocs&logoColor=white&label=pypi)](https://pypi.org/project/vultr-python/)
109
[![Deployments Pages](https://img.shields.io/github/deployments/cssnr/vultr-python/github-pages?logo=materialformkdocs&logoColor=white&label=github-pages)](https://cssnr.github.io/vultr-python/)
1110
[![GitHub Last Commit](https://img.shields.io/github/last-commit/cssnr/vultr-python?logo=github&label=updated)](https://github.com/cssnr/vultr-python/graphs/commit-activity)
1211
[![GitHub Repo Size](https://img.shields.io/github/repo-size/cssnr/vultr-python?logo=bookstack&logoColor=white&label=repo%20size)](https://github.com/cssnr/vultr-python)
@@ -120,6 +119,18 @@ sshkey = vultr.post('ssh-keys', name='key-name', ssh_key='ssh-rsa AAAA...')
120119
vultr.delete(f"instances/019ad1a8-2aa3-7650-83d1-8520d65ed6af")
121120
```
122121

122+
Errors Handling
123+
124+
```python
125+
from vultr import VultrException
126+
127+
try:
128+
instance = vultr.create_instance("atl", "vc2-1c-0.5gb-v6", os_id=2284)
129+
except VultrException as error:
130+
print(error.error) # 'Server add failed: Ubuntu 24.04 LTS x64 requires a plan with at least 1000 MB memory.'
131+
print(error.status) # 400
132+
```
133+
123134
Full Documentation: [https://cssnr.github.io/vultr-python](https://cssnr.github.io/vultr-python/)
124135

125136
Vultr API Reference: [https://www.vultr.com/api](https://www.vultr.com/api/?ref=6905748)

docs/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ sshkey = vultr.post('ssh-keys', name='key-name', ssh_key='ssh-rsa AAAA...')
9696
vultr.delete(f"instances/019ad1a8-2aa3-7650-83d1-8520d65ed6af")
9797
```
9898

99+
Errors Handling
100+
101+
```python
102+
from vultr import VultrException
103+
104+
try:
105+
instance = vultr.create_instance("atl", "vc2-1c-0.5gb-v6", os_id=2284)
106+
except VultrException as error:
107+
print(error.error) # 'Server add failed: Ubuntu 24.04 LTS x64 requires a plan with at least 1000 MB memory.'
108+
print(error.status) # 400
109+
```
110+
99111
 
100112

101113
Vultr API Reference: [https://www.vultr.com/api](https://www.vultr.com/api/?ref=6905748)

src/vultr/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.. include:: ../../docs/index.md
33
"""
44

5-
from .vultr import Vultr
5+
from .vultr import Vultr, VultrException
66

77

8-
__all__ = ["Vultr"]
8+
__all__ = ["Vultr", "VultrException"]

src/vultr/vultr.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,25 +158,25 @@ def filter_regions(regions: list, locations: list) -> list:
158158
def _get(self, url, params: Optional[dict] = None):
159159
r = self._session.get(url, params=params, timeout=10)
160160
if not r.ok:
161-
r.raise_for_status()
161+
raise VultrException(r)
162162
return r.json()
163163

164164
def _post(self, url, data):
165165
r = self._session.post(url, json=data, timeout=10)
166166
if not r.ok:
167-
r.raise_for_status()
167+
raise VultrException(r)
168168
return r.json()
169169

170170
def _patch(self, url, data):
171171
r = self._session.patch(url, json=data, timeout=10)
172172
if not r.ok:
173-
r.raise_for_status()
173+
raise VultrException(r)
174174
return r.json()
175175

176176
def _delete(self, url):
177177
r = self._session.delete(url, timeout=10)
178178
if not r.ok:
179-
r.raise_for_status()
179+
raise VultrException(r)
180180
return None
181181

182182
@staticmethod
@@ -190,3 +190,16 @@ def _get_obj_key(obj, key="id"):
190190
return obj[key]
191191
else:
192192
raise ValueError(f"Unable to parse object: {key}")
193+
194+
195+
class VultrException(Exception):
196+
def __init__(self, response: requests.Response):
197+
try:
198+
data = response.json()
199+
error = data.get("error", response.text)
200+
except ValueError:
201+
error = response.text
202+
status = response.status_code
203+
self.error = error
204+
self.status = status
205+
super().__init__(f"Error {self.status}: {self.error}")

0 commit comments

Comments
 (0)