Skip to content

Commit

Permalink
Address a review comment by a adding a comment about ImpalaHttpClient…
Browse files Browse the repository at this point in the history
… having

originated in Imapla Shell.
Add the message body into the HttpError so that it can be observed.
This isn't always present but it can be useful.
  • Loading branch information
bartash committed May 21, 2020
1 parent 80df326 commit 881910c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions impala/_thrift_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@
from RuntimeProfile import TRuntimeProfileFormat
ThriftClient = TClient


# ImpalaHttpClient is copied from Impala Shell.
# The implementations should be kept in sync as much as possible.
class ImpalaHttpClient(TTransportBase):
"""Http implementation of TTransport base."""

Expand Down Expand Up @@ -226,6 +227,9 @@ def setCustomHeaders(self, headers):
def read(self, sz):
return self.__http_response.read(sz)

def readBody(self):
return self.__http_response.read()

def write(self, buf):
self.__wbuf.write(buf)

Expand Down Expand Up @@ -283,7 +287,8 @@ def flush(self):
if self.code >= 300:
# Report any http response code that is not 1XX (informational response) or
# 2XX (successful).
raise HttpError(self.code, self.message)
body = self.readBody()
raise HttpError(self.code, self.message, body)


def get_socket(host, port, use_ssl, ca_cert):
Expand Down
4 changes: 3 additions & 1 deletion impala/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ class HiveServer2Error(RPCError):

class HttpError(RPCError):
"""An error containing an http response code"""
def __init__(self, code, message):
def __init__(self, code, message, body):
self.code = code
self.message = message
self.body = body

def __str__(self):
# Don't try to print the body as we don't know what format it is.
return "HTTP code {}: {}".format(self.code, self.message)


Expand Down
2 changes: 2 additions & 0 deletions impala/tests/test_http_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def do_POST(self):
# Respond with 503.
self.send_response(code=http_client.SERVICE_UNAVAILABLE, message="Service Unavailable")
self.end_headers()
self.wfile.write("extra text".encode('utf-8'))

class TestHTTPServer503(object):
def __init__(self):
Expand Down Expand Up @@ -88,6 +89,7 @@ def test_http_interactions(self, http_503_server):
except HttpError as e:
assert str(e) == "HTTP code 503: Service Unavailable"
assert e.code == http_client.SERVICE_UNAVAILABLE
assert e.body.decode("utf-8") == "extra text"


def get_unused_port():
Expand Down

0 comments on commit 881910c

Please sign in to comment.