Skip to content

Commit

Permalink
Merge 9ce999e into 337c05f
Browse files Browse the repository at this point in the history
  • Loading branch information
msabramo committed Feb 10, 2015
2 parents 337c05f + 9ce999e commit b352684
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ python:
script:
- make
after_success:
- pip install python-coveralls
- coveralls
- if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then pip install python-coveralls && coveralls; fi
15 changes: 12 additions & 3 deletions httpie/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def decode_args(args, stdin_encoding):
]


def main(args=sys.argv[1:], env=Environment()):
def main(args=sys.argv[1:], env=Environment(), error=None):
"""Run the main program and write the output to ``env.stdout``.
Return exit status code.
Expand All @@ -81,11 +81,14 @@ def main(args=sys.argv[1:], env=Environment()):
if env.config.default_options:
args = env.config.default_options + args

def error(msg, *args, **kwargs):
def _error(msg, *args, **kwargs):
msg = msg % args
level = kwargs.get('level', 'error')
env.stderr.write('\nhttp: %s: %s\n' % (level, msg))

if error is None:
error = _error

debug = '--debug' in args
traceback = debug or '--traceback' in args
exit_status = ExitStatus.OK
Expand Down Expand Up @@ -183,7 +186,13 @@ def error(msg, *args, **kwargs):
# Network errors vs. bugs, etc.
if traceback:
raise
error('%s: %s', type(e).__name__, str(e))
msg = str(e)
if hasattr(e, 'request'):
request = e.request
if hasattr(request, 'url'):
msg += ' while doing %s request to URL: %s' % (
request.method, request.url)
error('%s: %s', type(e).__name__, msg)
exit_status = ExitStatus.ERROR

finally:
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tox
mock
pytest
pytest-cov
pytest-httpbin
Expand Down
48 changes: 48 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import mock
from pytest import raises
from requests import Request, Timeout
from requests.exceptions import ConnectionError

from httpie.core import main

error_msg = None


@mock.patch('httpie.core.get_response')
def test_error(get_response):
def error(msg, *args, **kwargs):
global error_msg
error_msg = msg % args

exc = ConnectionError('Connection aborted')
exc.request = Request(method='GET', url='http://www.google.com')
get_response.side_effect = exc
ret = main(['--ignore-stdin', 'www.google.com'], error=error)
assert ret == 1
assert error_msg == (
'ConnectionError: '
'Connection aborted while doing GET request to URL: '
'http://www.google.com')


@mock.patch('httpie.core.get_response')
def test_error_traceback(get_response):
exc = ConnectionError('Connection aborted')
exc.request = Request(method='GET', url='http://www.google.com')
get_response.side_effect = exc
with raises(ConnectionError):
ret = main(['--ignore-stdin', '--traceback', 'www.google.com'])


@mock.patch('httpie.core.get_response')
def test_timeout(get_response):
def error(msg, *args, **kwargs):
global error_msg
error_msg = msg % args

exc = Timeout('Request timed out')
exc.request = Request(method='GET', url='http://www.google.com')
get_response.side_effect = exc
ret = main(['--ignore-stdin', 'www.google.com'], error=error)
assert ret == 2
assert error_msg == 'Request timed out (30s).'

0 comments on commit b352684

Please sign in to comment.