From 98e320a1a34c0c6d552a98cff83b466c3f498630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Roztoc=CC=8Cil?= Date: Sun, 26 Feb 2012 16:13:12 +0100 Subject: [PATCH] Added error handling. --- httpie/httpie.py | 34 +++++++++++++++++++++++----------- httpie/pretty.py | 4 +++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/httpie/httpie.py b/httpie/httpie.py index 04e95875..33b98b15 100755 --- a/httpie/httpie.py +++ b/httpie/httpie.py @@ -56,6 +56,9 @@ group_type.add_argument('--form', '-f', action='store_true', ' if not specified.') # Output options. +parser.add_argument('--traceback', action='store_true', default=False, + help='Print a full exception traceback should one' + ' be raised by `requests`.') parser.add_argument('--ugly', '-u', help='Do not prettify the response.', dest='prettify', action='store_false', default=True) group_only = parser.add_mutually_exclusive_group(required=False) @@ -131,17 +134,26 @@ def main(): headers['Content-Type'] = TYPE_FORM # Fire the request. - response = requests.request( - method=args.method.lower(), - url=args.url if '://' in args.url else 'http://%s' % args.url, - headers=headers, - data=data, - verify=True if args.verify == 'yes' else args.verify, - timeout=args.timeout, - auth=(args.auth.key, args.auth.value) if args.auth else None, - proxies={proxy.key: proxy.value for proxy in args.proxy}, - files={os.path.basename(f.name): f for f in args.file} - ) + try: + response = requests.request( + method=args.method.lower(), + url=args.url if '://' in args.url else 'http://%s' % args.url, + headers=headers, + data=data, + verify=True if args.verify == 'yes' else args.verify, + timeout=args.timeout, + auth=(args.auth.key, args.auth.value) if args.auth else None, + proxies={proxy.key: proxy.value for proxy in args.proxy}, + files={os.path.basename(f.name): f for f in args.file} + ) + except (KeyboardInterrupt, SystemExit) as e: + sys.stderr.write('\n') + sys.exit(1) + except Exception as e: + if args.traceback: + raise + sys.stderr.write(str(e.message) + '\n') + sys.exit(1) # Display the response. original = response.raw._original_response diff --git a/httpie/pretty.py b/httpie/pretty.py index 07907f7c..db463347 100644 --- a/httpie/pretty.py +++ b/httpie/pretty.py @@ -5,6 +5,7 @@ from pygments.lexers import get_lexer_for_mimetype from pygments.formatters.terminal256 import Terminal256Formatter from pygments.lexer import RegexLexer, bygroups from pygments import token +from . import solarized TYPE_JS = 'application/javascript' @@ -24,7 +25,8 @@ class HTTPLexer(RegexLexer): highlight = partial(pygments.highlight, - formatter=Terminal256Formatter(style='native')) + formatter=Terminal256Formatter( + style=solarized.SolarizedStyle)) highlight_http = partial(highlight, lexer=HTTPLexer())