mirror of
https://github.com/httpie/cli.git
synced 2024-11-28 08:38:44 +02:00
Improved --debug.
This commit is contained in:
parent
49e44d9b7e
commit
1fbe7a6121
@ -3,7 +3,7 @@ HTTPie - cURL for humans.
|
||||
|
||||
"""
|
||||
__author__ = 'Jakub Roztocil'
|
||||
__version__ = '0.2.7'
|
||||
__version__ = '0.2.7dev'
|
||||
__licence__ = 'BSD'
|
||||
|
||||
|
||||
|
@ -220,17 +220,23 @@ parser.add_argument(
|
||||
''')
|
||||
)
|
||||
parser.add_argument(
|
||||
'--timeout', type=float,
|
||||
'--timeout', type=float, default=30,
|
||||
help=_('''
|
||||
Float describes the timeout of the request
|
||||
(Use socket.setdefaulttimeout() as fallback).
|
||||
The timeout of the request in seconds. The default value is 30
|
||||
seconds.
|
||||
''')
|
||||
)
|
||||
parser.add_argument(
|
||||
'--traceback', action='store_true', default=False,
|
||||
help=_('''
|
||||
Prints exception traceback should one occur.
|
||||
''')
|
||||
)
|
||||
parser.add_argument(
|
||||
'--debug', action='store_true', default=False,
|
||||
help=_('''
|
||||
Prints exception traceback should one occur and other
|
||||
information useful for debugging HTTPie itself.
|
||||
Prints exception traceback should one occur, and also other
|
||||
information useful for debugging HTTPie itself and bug reports.
|
||||
''')
|
||||
)
|
||||
|
||||
|
@ -13,10 +13,14 @@ Invocation flow:
|
||||
import sys
|
||||
import json
|
||||
import errno
|
||||
from pprint import pformat
|
||||
|
||||
import requests
|
||||
import requests.auth
|
||||
from requests.compat import str
|
||||
from httpie import __version__ as httpie_version
|
||||
from requests import __version__ as requests_version
|
||||
from pygments import __version__ as pygments_version
|
||||
|
||||
from .cli import parser
|
||||
from .models import Environment
|
||||
@ -28,7 +32,7 @@ FORM = 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
JSON = 'application/json; charset=utf-8'
|
||||
|
||||
|
||||
def get_response(args):
|
||||
def get_requests_kwargs(args):
|
||||
"""Send the request and return a `request.Response`."""
|
||||
|
||||
auto_json = args.data and not args.form
|
||||
@ -58,20 +62,25 @@ def get_response(args):
|
||||
'digest': requests.auth.HTTPDigestAuth,
|
||||
}[args.auth_type](args.auth.key, args.auth.value)
|
||||
|
||||
return requests.request(
|
||||
prefetch=False,
|
||||
method=args.method.lower(),
|
||||
url=args.url,
|
||||
headers=args.headers,
|
||||
data=args.data,
|
||||
verify={'yes': True, 'no': False}.get(args.verify, args.verify),
|
||||
timeout=args.timeout,
|
||||
auth=credentials,
|
||||
proxies=dict((p.key, p.value) for p in args.proxy),
|
||||
files=args.files,
|
||||
allow_redirects=args.allow_redirects,
|
||||
params=args.params,
|
||||
)
|
||||
kwargs = {
|
||||
'prefetch': False,
|
||||
'method': args.method.lower(),
|
||||
'url': args.url,
|
||||
'headers': args.headers,
|
||||
'data': args.data,
|
||||
'verify': {
|
||||
'yes': True,
|
||||
'no': False
|
||||
}.get(args.verify,args.verify),
|
||||
'timeout': args.timeout,
|
||||
'auth': credentials,
|
||||
'proxies': dict((p.key, p.value) for p in args.proxy),
|
||||
'files': args.files,
|
||||
'allow_redirects': args.allow_redirects,
|
||||
'params': args.params
|
||||
}
|
||||
|
||||
return kwargs
|
||||
|
||||
|
||||
def get_exist_status(code, allow_redirects=False):
|
||||
@ -101,11 +110,23 @@ def main(args=sys.argv[1:], env=Environment()):
|
||||
env.stderr.write('\nhttp: error: %s\n' % msg)
|
||||
|
||||
debug = '--debug' in args
|
||||
traceback = debug or '--traceback' in args
|
||||
status = EXIT.OK
|
||||
|
||||
if debug:
|
||||
sys.stderr.write('HTTPie version: %s\n' % httpie_version)
|
||||
sys.stderr.write('Requests version: %s\n' % requests_version)
|
||||
sys.stderr.write('Pygments version: %s\n' % pygments_version)
|
||||
|
||||
try:
|
||||
args = parser.parse_args(args=args, env=env)
|
||||
response = get_response(args)
|
||||
kwargs = get_requests_kwargs(args)
|
||||
|
||||
if args.debug:
|
||||
sys.stderr.write(
|
||||
'\n>>> requests.request(%s)\n\n' % pformat(kwargs))
|
||||
|
||||
response = requests.request(**kwargs)
|
||||
|
||||
if args.check_status:
|
||||
status = get_exist_status(response.status_code,
|
||||
@ -121,14 +142,14 @@ def main(args=sys.argv[1:], env=Environment()):
|
||||
flush=env.stdout_isatty or args.stream)
|
||||
|
||||
except IOError as e:
|
||||
if not debug and e.errno == errno.EPIPE:
|
||||
if not traceback and e.errno == errno.EPIPE:
|
||||
# Ignore broken pipes unless --debug.
|
||||
env.stderr.write('\n')
|
||||
else:
|
||||
raise
|
||||
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
if debug:
|
||||
if traceback:
|
||||
raise
|
||||
env.stderr.write('\n')
|
||||
status = EXIT.ERROR
|
||||
@ -136,7 +157,7 @@ def main(args=sys.argv[1:], env=Environment()):
|
||||
except Exception as e:
|
||||
# TODO: distinguish between expected and unexpected errors.
|
||||
# network errors vs. bugs, etc.
|
||||
if debug:
|
||||
if traceback:
|
||||
raise
|
||||
error('%s: %s', type(e).__name__, str(e))
|
||||
status = EXIT.ERROR
|
||||
|
@ -101,6 +101,9 @@ class Parser(argparse.ArgumentParser):
|
||||
|
||||
args = super(Parser, self).parse_args(args, namespace)
|
||||
|
||||
if args.debug:
|
||||
args.traceback = True
|
||||
|
||||
if args.output:
|
||||
env.stdout = args.output
|
||||
env.stdout_isatty = False
|
||||
@ -171,7 +174,7 @@ class Parser(argparse.ArgumentParser):
|
||||
0, KeyValueArgType(*SEP_GROUP_ITEMS).__call__(args.url))
|
||||
|
||||
except argparse.ArgumentTypeError as e:
|
||||
if args.debug:
|
||||
if args.traceback:
|
||||
raise
|
||||
self.error(e.message)
|
||||
|
||||
@ -201,7 +204,7 @@ class Parser(argparse.ArgumentParser):
|
||||
files=args.files,
|
||||
params=args.params)
|
||||
except ParseError as e:
|
||||
if args.debug:
|
||||
if args.traceback:
|
||||
raise
|
||||
self.error(e.message)
|
||||
|
||||
|
@ -142,7 +142,7 @@ def http(*args, **kwargs):
|
||||
try:
|
||||
|
||||
try:
|
||||
exit_status = main(args=['--debug'] + list(args), **kwargs)
|
||||
exit_status = main(args=['--traceback'] + list(args), **kwargs)
|
||||
except Exception:
|
||||
sys.stderr.write(env.stderr.read())
|
||||
raise
|
||||
|
Loading…
Reference in New Issue
Block a user