mirror of
				https://github.com/python-kasa/python-kasa.git
				synced 2025-11-03 22:22:06 +00:00 
			
		
		
		
	Pretty-print all exceptions from cli commands (#428)
Print out the repr of captured exception instead of full traceback
This commit is contained in:
		
							
								
								
									
										16
									
								
								kasa/cli.py
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								kasa/cli.py
									
									
									
									
									
								
							@@ -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",
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user