Pretty-print all exceptions from cli commands (#428)

Print out the repr of captured exception instead of full traceback
This commit is contained in:
Teemu R 2023-02-18 17:32:03 +01:00 committed by GitHub
parent dd044130d4
commit 92636fe82d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,7 +52,21 @@ click.anyio_backend = "asyncio"
pass_dev = click.make_pass_decorator(SmartDevice)
@click.group(invoke_without_command=True)
class ExceptionHandlerGroup(click.Group):
"""Group to capture all exceptions and print them nicely.
Idea from https://stackoverflow.com/a/44347763
"""
def __call__(self, *args, **kwargs):
"""Run the coroutine in the event loop and print any exceptions."""
try:
asyncio.get_event_loop().run_until_complete(self.main(*args, **kwargs))
except Exception as ex:
click.echo(f"Got error: {ex!r}")
@click.group(invoke_without_command=True, cls=ExceptionHandlerGroup)
@click.option(
"--host",
envvar="KASA_HOST",