From aa076ee53bb1c57a25912b4a7507ce8bbe3c4fbb Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Wed, 14 Mar 2018 09:53:33 -0700 Subject: [PATCH] composer-cli: Handle download errors The API will return an error 400 with a json error object if the download fails, catch this and report the error to the user without a traceback. --- src/composer/cli/__init__.py | 6 +++++- src/composer/http_client.py | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/composer/cli/__init__.py b/src/composer/cli/__init__.py index e4850af8..97c7a9fc 100644 --- a/src/composer/cli/__init__.py +++ b/src/composer/cli/__init__.py @@ -49,4 +49,8 @@ def main(opts): log.error("Missing %s sub-command", opts.args[0]) return 1 else: - return command_map[opts.args[0]](opts) + try: + return command_map[opts.args[0]](opts) + except Exception as e: + log.error(str(e)) + return 1 diff --git a/src/composer/http_client.py b/src/composer/http_client.py index 658bf0bb..1b3ea586 100644 --- a/src/composer/http_client.py +++ b/src/composer/http_client.py @@ -158,6 +158,10 @@ def download_file(socket_path, url, progress=True): """ http = UnixHTTPConnectionPool(socket_path) r = http.request("GET", url, preload_content=False) + if r.status == 400: + err = json.loads(r.data.decode("utf-8")) + if not err["status"]: + raise RuntimeError(err["error"]["msg"]) filename = get_filename(r) if os.path.exists(filename):