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

Add HTTP status code to HttpResponseError #116

Merged
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(
tomerm-iguazio marked this conversation as resolved.
Show resolved Hide resolved
"Request failed with status {0}: {1}".format(self.status_code, self.body), status_code=self.status_code
)


class Responses(object):
Expand Down
Loading