Skip to content

Commit

Permalink
Add HTTP status code to HttpResponseError (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomerm-iguazio authored Feb 13, 2024
1 parent ff4ce9e commit 64d4799
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 12 additions & 0 deletions tests/test_client_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import v3io.dataplane.client
import v3io.dataplane.transport.httpclient
from v3io.dataplane.object import Model
from v3io.dataplane.response import HttpResponseError


class MockResponse:
Expand Down Expand Up @@ -107,3 +109,13 @@ def test_error_on_use_after_close():
client.close()
with pytest.raises(RuntimeError):
client.object.get("doesntexist", "doesntexist", raise_for_status=v3io.dataplane.RaiseForStatus.never)


@pytest.mark.parametrize("object_function", [Model.get, Model.put, Model.delete])
def test_raise_http_response_error(object_function):
client = v3io.dataplane.Client(
transport_kind="httpclient",
)
with pytest.raises(HttpResponseError, match="Container not found") as response_error:
object_function(client.object, "not-exists", "path/to/object")
assert response_error.value.status_code == 404
14 changes: 11 additions & 3 deletions v3io/dataplane/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
class HttpResponseError(Exception):
"""Exception raised on bad http status"""

pass
def __init__(self, message, status_code):
super().__init__(message)
self.status_code = status_code

def __repr__(self):
return f"HttpResponseError('{self}', {self.status_code})"


class Response(object):
Expand All @@ -49,7 +54,8 @@ def output(self):
except Exception:
raise HttpResponseError(
f"Failed to parse response with status {self.status_code}, "
f"body {self.body}, headers={self.headers}"
f"body {self.body}, headers={self.headers}",
self.status_code,
)

self._parsed_output = self._output(parsed_output)
Expand All @@ -67,7 +73,9 @@ def raise_for_status(self, expected_statuses=None):
if (expected_statuses is None and self.status_code >= 300) or (
expected_statuses and self.status_code not in expected_statuses
):
raise HttpResponseError("Request failed with status {0}: {1}".format(self.status_code, self.body))
raise HttpResponseError(
"Request failed with status {0}: {1}".format(self.status_code, self.body), status_code=self.status_code
)


class Responses(object):
Expand Down

0 comments on commit 64d4799

Please sign in to comment.