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

Error handling #62

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions exoscale/api/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# exceptions.py


elkezza marked this conversation as resolved.
Show resolved Hide resolved
class ExoscaleAPIException(Exception):
"""
Base exception for exoscale API errors.
Base for all other exception classes, it allows to catch all exoscale exceptions with a single except block.
"""

pass


class ExoscaleAPIClientException(ExoscaleAPIException):
"""
For client-side errors (4xx).
Shows that the client sent a bad request.
"""

pass


class ExoscaleAPIServerException(ExoscaleAPIException):
"""
For server-side errors (5xx).
Shows the server encountered an error while processing the request.
"""

pass
12 changes: 12 additions & 0 deletions exoscale/api/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pathlib import Path

from exoscale_auth import ExoscaleV2Auth
from .exceptions import ExoscaleAPIClientException, ExoscaleAPIServerException

import requests

Expand Down Expand Up @@ -155,6 +156,17 @@ def _call_operation(self, operation_id, parameters=None, body=None):
response = self.session.request(
method=op["verb"].upper(), url=url, params=query_params, **json
)

# Error handling
if response.status_code >= 400 and response.status_code < 500:
elkezza marked this conversation as resolved.
Show resolved Hide resolved
raise ExoscaleAPIClientException(
f"Client error {response.status_code}: {response.text}"
)
elif response.status_code >= 500:
raise ExoscaleAPIServerException(
f"Server error {response.status_code}: {response.text}"
)

return response.json()


Expand Down