Using `emoji.emojize()` when handling errors and exceptions in Python
While building a new Python tool, I wanted some pretty emoji in the things that get printed while doing error handling with try
and except
in a click
app. Assuming you have done import emoji
, you can do that with something like this:
except requests.exceptions.SSLError as e:
click.echo(emoji.emojize(f":fox: SSL Error: {str(e)} Check the cert."))
except requests.exceptions.RequestException as e:
click.echo(emoji.emojize(f":warning: : {str(e)}"))
Alternately:
except gitlab.exceptions.GitlabError:
raise click.ClickException(emoji.emojize(f":fox: GitLab API error"))
This works with raise
, too:
if not sku or not device:
raise ValueError(emoji.emojize(":crying_cat: Something is missing."))