From f56e87f100989554cd17ddd8dbf2d666a56ed468 Mon Sep 17 00:00:00 2001 From: Cédric Picard Date: Sat, 29 Nov 2014 10:41:39 +0100 Subject: Better error management --- scripts/pastit.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'scripts') diff --git a/scripts/pastit.py b/scripts/pastit.py index 63e5ab3..29aa3cd 100755 --- a/scripts/pastit.py +++ b/scripts/pastit.py @@ -25,8 +25,8 @@ from docopt import docopt def post(text, url): try: req = requests.post(url, data={"content":text}) - except requests.exceptions.ConnectionError as e: - sys.exit(e) + except BaseException as e: + manage(e, url) return req.url @@ -36,8 +36,8 @@ def get(url): try: req = requests.get(url) - except requests.exceptions.ConnectionError as e: - sys.exit(e) + except BaseException as e: + manage(e, url) if req.status_code == 404: return "Paste not found" @@ -47,6 +47,20 @@ def get(url): return req.text +def manage(excep, url=None): + try: + raise excep + except requests.exceptions.ConnectionError as e: + sys.exit(e) + except (requests.exceptions.InvalidURL, + requests.exceptions.InvalidSchema) as e: + print("Invalid URL: %s" % url, file=sys.stderr) + if not url.startswith("http"): + print("Perhaps you meant http://%s" % url, file=sys.stderr) + sys.exit(1) + + + def main(): args = docopt(__doc__) -- cgit v1.2.3