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

Decode the error response if it is in byte format #193

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ exclude =
build
import-order-style = google
max-line-length = 90

per-file-ignores =
tests/test_client.py: E501 W291
3 changes: 3 additions & 0 deletions pydruid/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ def _post(self, query):
if e.code == 500:
# has Druid returned an error?
try:
if isinstance(err, bytes):
# Decode the error before serialize it to JSON
err = err.decode("utf-8")
err = json.loads(err)
except ValueError:
if HTML_ERROR.search(err):
Expand Down
33 changes: 30 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: UTF-8 -*-
import textwrap
import urllib
from io import StringIO
from io import BytesIO, StringIO
from unittest.mock import Mock, patch

import pytest
Expand All @@ -22,7 +22,10 @@ def create_blank_query():

def _http_error(code, msg, data=""):
# Need a file-like object for the response data
fp = StringIO(data)
if isinstance(data, bytes):
fp = BytesIO(data)
else:
fp = StringIO(data)
return urllib.error.HTTPError(
url="http://fakeurl:8080/druid/v2/", hdrs={}, code=code, msg=msg, fp=fp
)
Expand Down Expand Up @@ -119,6 +122,29 @@ def test_druid_returns_html_error(self, mock_urlopen):
).strip()
)

@patch("pydruid.client.urllib.request.urlopen")
def test_druid_returns_string_error_bytes_error_response(self, mock_urlopen):
# given
message = b"Error as bytes, please decode me"
mock_urlopen.side_effect = _http_error(500, "Internal Server Error", message)
client = create_client()

# when / then
with pytest.raises(IOError) as e:
client.topn(
datasource="testdatasource",
granularity="all",
intervals="2015-12-29/pt1h",
aggregations={"count": doublesum("count")},
dimension="user_name",
metric="count",
filter=Dimension("user_lang") == "en",
threshold=1,
context={"timeout": 1000},
)

assert "Error as bytes, please decode me" in str(e.value)

@patch("pydruid.client.urllib.request.urlopen")
def test_druid_returns_results(self, mock_urlopen):
# given
Expand Down Expand Up @@ -185,7 +211,8 @@ def test_client_allows_to_export_last_query(self, mock_urlopen):
)

# when / then
# assert that last_query.export_tsv method was called (it should throw an exception, given empty path)
# assert that last_query.export_tsv method was called (it should throw
# an exception, given empty path)
with pytest.raises(TypeError):
client.export_tsv(None)

Expand Down