diff --git a/python/CHANGELOG.D/365.feature b/python/CHANGELOG.D/365.feature new file mode 100644 index 000000000..e5df2a14c --- /dev/null +++ b/python/CHANGELOG.D/365.feature @@ -0,0 +1 @@ +Implement --show-traceback command line option to show python traceback in case of top-level error \ No newline at end of file diff --git a/python/README.md b/python/README.md index 1b7ed8ac7..b78d84eb3 100644 --- a/python/README.md +++ b/python/README.md @@ -60,6 +60,7 @@ Name | Description| |_\-u, --url URL_|Override API URL \[default: https://platform.dev.neuromation.io/api/v1]| |_\-t, --token TOKEN_|API authentication token \(not implemented)| |_--verbose_|Enable verbose logging| +|_\--show-traceback_|Show Python traceback on exception| |_\-v, --version_|Print version and exit| diff --git a/python/neuromation/cli/main.py b/python/neuromation/cli/main.py index 842ac5339..9bc83cd85 100644 --- a/python/neuromation/cli/main.py +++ b/python/neuromation/cli/main.py @@ -73,7 +73,7 @@ def setup_console_handler(handler, verbose, noansi=False): @command -def neuro(url, token, verbose, version): +def neuro(url, token, verbose, show_traceback, version): """ ◣ ▇ ◣ ▇ ◥ ◣ @@ -92,6 +92,7 @@ def neuro(url, token, verbose, version): -u, --url URL Override API URL [default: {api_url}] -t, --token TOKEN API authentication token (not implemented) --verbose Enable verbose logging + --show-traceback Show Python traceback on exception -v, --version Print version and exit Commands: @@ -883,6 +884,13 @@ def main(): if is_verbose: sys.argv.remove("--verbose") + is_show_traceback = "--show-traceback" in sys.argv + if is_show_traceback: + sys.argv.remove("--show-traceback") + log_error = log.exception + else: + log_error = log.error + setup_logging() setup_console_handler(console_handler, verbose=is_verbose) @@ -906,51 +914,51 @@ def main(): print(res) except neuromation.client.IllegalArgumentError as error: - log.error(f"Illegal argument(s) ({error})") + log_error(f"Illegal argument(s) ({error})") sys.exit(os.EX_DATAERR) except neuromation.client.ResourceNotFound as error: - log.error(f"{error}") + log_error(f"{error}") sys.exit(os.EX_OSFILE) except neuromation.client.AuthenticationError as error: - log.error(f"Cannot authenticate ({error})") + log_error(f"Cannot authenticate ({error})") sys.exit(os.EX_NOPERM) except neuromation.client.AuthorizationError as error: - log.error(f"You haven`t enough permission ({error})") + log_error(f"You haven`t enough permission ({error})") sys.exit(os.EX_NOPERM) except neuromation.client.ClientError as error: - log.error(f"Application error ({error})") + log_error(f"Application error ({error})") sys.exit(os.EX_SOFTWARE) except aiohttp.ClientError as error: - log.error(f"Connection error ({error})") + log_error(f"Connection error ({error})") sys.exit(os.EX_IOERR) except NotImplementedError as error: - log.error(f"{error}") + log_error(f"{error}") sys.exit(os.EX_SOFTWARE) except FileNotFoundError as error: - log.error(f"File not found ({error})") + log_error(f"File not found ({error})") sys.exit(os.EX_OSFILE) except NotADirectoryError as error: - log.error(f"{error}") + log_error(f"{error}") sys.exit(os.EX_OSFILE) except PermissionError as error: - log.error(f"Cannot access file ({error})") + log_error(f"Cannot access file ({error})") sys.exit(os.EX_NOPERM) except IOError as error: - log.error(f"I/O Error ({error})") + log_error(f"I/O Error ({error})") raise error except KeyboardInterrupt: - log.error("Aborting.") + log_error("Aborting.") sys.exit(130) except ValueError as e: print(e) sys.exit(127) except Exception as e: - log.error(f"{e}") + log_error(f"{e}") raise e diff --git a/python/tests/cli/test_help.py b/python/tests/cli/test_help.py index 4164f8b6a..39f8bd5df 100644 --- a/python/tests/cli/test_help.py +++ b/python/tests/cli/test_help.py @@ -13,7 +13,7 @@ def test_help(run): assert not captured.err assert captured.out == neuro.__doc__ + "\n" - commands = neuro(None, None, None, None) + commands = neuro(None, None, None, None, None) for command, func in commands.items(): if not hasattr(func, "_command_name"):