Skip to content

Commit

Permalink
Add elapsed time to HTTP errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kalefranz committed Nov 24, 2016
1 parent 48c8be3 commit a1a1181
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions conda/_vendor/auxlib/logz.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pprint import pformat
from sys import stderr

from .compat import text_type

log = getLogger(__name__)
root_log = getLogger()

Expand Down Expand Up @@ -114,6 +116,8 @@ def requests_models_Response_builder(builder, response_object):
builder.extend("> {0}: {1}".format(key, value)
for key, value in sorted(response_object.headers.items(),
key=response_header_sort_key))
elapsed = text_type(response_object.elapsed).split(':', 1)[-1]
builder.append('> Elapsed: {0}'.format(elapsed))
builder.append('')
content_type = response_object.headers.get('Content-Type')
if content_type == 'application/json':
Expand Down
7 changes: 5 additions & 2 deletions conda/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,11 @@ def get_json_str(filename, resp_content):
else:
help_message = "An HTTP error occurred when trying to retrieve this URL.\n%r" % e

raise CondaHTTPError(help_message, e.response.url if e.response else None, status_code,
e.response.reason if e.response else None)
raise CondaHTTPError(help_message,
getattr(e.response, 'url', None),
status_code,
getattr(e.response, 'reason', None),
getattr(e.response, 'elapsed', None))

cache['_url'] = url
try:
Expand Down
12 changes: 8 additions & 4 deletions conda/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

from datetime import timedelta

import logging
import sys
from logging import getLogger
Expand Down Expand Up @@ -237,14 +239,16 @@ def __init__(self, package_name, message, *args):


class CondaHTTPError(CondaError):
def __init__(self, message, url, status_code, reason):
def __init__(self, message, url, status_code, reason, elapsed_time):
message = dals("""
HTTP %(status_code)s %(reason)s
for url <%(url)s>
HTTP %(status_code)s %(reason)s for url <%(url)s>
Elapsed: %(elapsed_time)s
""") + message
if isinstance(elapsed_time, timedelta):
elapsed_time = text_type(elapsed_time).split(':', 1)[-1]
super(CondaHTTPError, self).__init__(message, url=url, status_code=status_code,
reason=reason)
reason=reason, elapsed_time=elapsed_time)


class CondaRevisionError(CondaError):
Expand Down

0 comments on commit a1a1181

Please sign in to comment.