Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

HH-33588 always log response.body on parse errors

  • Loading branch information...
commit 84df20089f31d243be92f619603f5872c407ad92 1 parent a90a882
@SuminAndrew SuminAndrew authored
Showing with 58 additions and 50 deletions.
  1. +58 −50 frontik/handler_debug.py
View
108 frontik/handler_debug.py
@@ -17,6 +17,7 @@
log = logging.getLogger('XML_debug')
+
def response_to_xml(response):
time_info = etree.Element('time_info')
content_type = response.headers.get('Content-Type', '')
@@ -27,15 +28,19 @@ def response_to_xml(response):
charset = 'utf-8'
def try_decode_body(charset):
- try:
- body = response.body.decode(charset)
- except UnicodeDecodeError:
+ decoded_body = None
+ try_charsets = (charset, 'cp1251')
+ for c in try_charsets:
try:
- body = response.body.decode('cp1251')
- except UnicodeDecodeError as e:
- log.exception('Cant decode body response')
- body = 'Cant decode body response'
- return body
+ decoded_body = response.body.decode(c)
+ break
+ except UnicodeDecodeError:
+ continue
+
+ if decoded_body is None:
@katraev Collaborator
katraev added a note

можно в else блок for вытащить. а можно и не вытащить

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ log.error('Could not decode response body (tried: %s)', ', '.join(try_charsets))
+ decoded_body = repr(response.body)
@katraev Collaborator
katraev added a note

обернуть try\except здесь и ниже

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return decoded_body
try:
if 'text/html' in content_type:
@@ -48,95 +53,99 @@ def try_decode_body(charset):
body = try_decode_body(charset)
else:
body = etree.fromstring(response.body)
- except Exception as e:
+ except Exception:
log.exception('Cant parse response body')
- body = 'Cant show response body'
+ body = repr(response.body)
try:
for name, value in response.time_info.iteritems():
time_info.append(E.time(str(value), name=name))
- except Exception as e:
+ except Exception:
log.exception('Cant append time info')
try:
response = E.response(
- E.body(body, content_type=content_type),
- E.code(str(response.code)),
- E.effective_url(response.effective_url),
- E.error(str(response.error)),
- E.size(str(len(response.body)) if response.body is not None else '0'),
- E.request_time(str(int(response.request_time * 1000))),
- _headers_to_xml(response.headers),
- time_info,
- )
- except Exception as e:
+ E.body(body, content_type=content_type),
+ E.code(str(response.code)),
+ E.effective_url(response.effective_url),
+ E.error(str(response.error)),
+ E.size(str(len(response.body)) if response.body is not None else '0'),
+ E.request_time(str(int(response.request_time * 1000))),
+ _headers_to_xml(response.headers),
+ time_info
+ )
+ except Exception:
log.exception('Cant log response info')
response = E.response(E.body('Cant log response info'))
return response
+
def request_to_xml(request):
- content_type = request.headers.get('Content-Type','')
- body = etree.Element("body", content_type = content_type)
+ content_type = request.headers.get('Content-Type', '')
+ body = etree.Element("body", content_type=content_type)
if request.body:
try:
if 'json' in content_type:
- body.text = json.dumps(json.loads(request.body), sort_keys = True, indent = 4)
+ body.text = json.dumps(json.loads(request.body), sort_keys=True, indent=4)
elif 'protobuf' in content_type:
body.text = request.body.encode('hex')
else:
body_query = urlparse.parse_qs(str(request.body), True)
for name, values in body_query.iteritems():
for value in values:
- body.append(E.param(value.decode("utf-8"), name = name))
- except Exception as e:
+ body.append(E.param(value.decode("utf-8"), name=name))
+ except Exception:
log.exception('Cant parse request body')
- body.text = 'Cant show request body'
+ body.text = repr(request.body)
try:
request = E.request(
- body,
- E.connect_timeout(str(request.connect_timeout)),
- E.follow_redirects(str(request.follow_redirects)),
- E.max_redirects(str(request.max_redirects)),
- E.method(request.method),
- E.request_timeout(str(request.request_timeout)),
- _params_to_xml(request.url),
- E.url(request.url),
- _headers_to_xml(request.headers),
- _cookies_to_xml(request.headers),
- E.meta(
- E.start_time(
- str(request.start_time)
- ))
- )
- except Exception as e:
+ body,
+ E.connect_timeout(str(request.connect_timeout)),
+ E.follow_redirects(str(request.follow_redirects)),
+ E.max_redirects(str(request.max_redirects)),
+ E.method(request.method),
+ E.request_timeout(str(request.request_timeout)),
+ _params_to_xml(request.url),
+ E.url(request.url),
+ _headers_to_xml(request.headers),
+ _cookies_to_xml(request.headers),
+ E.meta(
+ E.start_time(
+ str(request.start_time)
+ ))
+ )
+ except Exception:
log.exception('Cant parse request body')
- body.text = 'Cant show request body'
+ body.text = repr(request.body)
request = E.request(body)
return request
+
def _params_to_xml(url):
params = etree.Element('params')
query = frontik.util.get_query_parameters(url)
for name, values in query.iteritems():
for value in values:
- params.append(E.param(unicode(value, 'utf-8'), name = name))
+ params.append(E.param(unicode(value, 'utf-8'), name=name))
return params
+
def _headers_to_xml(request_or_response_headers):
headers = etree.Element('headers')
for name, value in request_or_response_headers.iteritems():
if name != 'Cookie':
- headers.append(E.header(str(value), name = name))
+ headers.append(E.header(str(value), name=name))
return headers
+
def _cookies_to_xml(request_headers):
cookies = etree.Element('cookies')
if 'Cookie' in request_headers:
_cookies = Cookie.SimpleCookie(request_headers['Cookie'])
for cookie in _cookies:
- cookies.append(E.cookie(_cookies[cookie].value, name = cookie))
+ cookies.append(E.cookie(_cookies[cookie].value, name=cookie))
return cookies
@@ -198,7 +207,6 @@ def handle(self, record):
self.log_data.append(record._stages)
-
class PageHandlerDebug(object):
INHERIT_DEBUG_HEADER_NAME = 'X-Inherit-Debug'
@@ -293,8 +301,8 @@ def get_debug_page(self, status_code, response_headers, original_response=None,
debug_log_data.append(frontik.xml_util.dict_to_xml(original_response, 'original-response'))
# show debug page if apply_xsl=True ('noxsl' flag is not set)
- # if debug mode is disabled, than we could have got there only after an exception — apply xsl anyway
- # if debug mode is inherited (through X-Inherit-Debug request header), than the response is always xml
+ # if debug mode is disabled, then we could have got there only after an exception — apply xsl anyway
+ # if debug mode is inherited (through X-Inherit-Debug request header), then the response is always xml
can_apply_xsl_or_500 = self.handler.xml.apply_xsl or not self.debug_mode.enabled
if can_apply_xsl_or_500 and not self.debug_mode.inherited:
try:
Please sign in to comment.
Something went wrong with that request. Please try again.