| 1 |
Exception Handling |
| 2 |
================== |
| 3 |
|
| 4 |
Properly handling exceptions helps you build plugins that don't crash or |
| 5 |
produce unintended side-effects when the user or your code does |
| 6 |
something you did not expect. Combined with logging, exceptions also |
| 7 |
allow you to get visibility of areas in which your bot is failing and |
| 8 |
ultimately address problems to improve user experience. |
| 9 |
|
| 10 |
Exceptions in Errbot plugins should be handled slightly differently from |
| 11 |
how exceptions are normally used in Python. When an unhandled exception |
| 12 |
is raised during the execution of a command, Errbot sends a message like |
| 13 |
this: |
| 14 |
|
| 15 |
.. code-block:: none |
| 16 |
|
| 17 |
Computer says nooo. See logs for details: |
| 18 |
<exception message here> |
| 19 |
|
| 20 |
The above is neither helpful nor user-friendly, as the exception message |
| 21 |
may be too technical or brief (notice there is no traceback) for the |
| 22 |
user to understand. Even if you were to provide your own exception |
| 23 |
message, the "Computer says nooo ..." part is neither particularly |
| 24 |
attractive or informative. |
| 25 |
|
| 26 |
When handling exceptions, follow these steps: |
| 27 |
|
| 28 |
* trap the exception as you usually would |
| 29 |
* log the exception inside of the ``except`` block |
| 30 |
|
| 31 |
* ``self.log.exception('Descriptive message here')`` |
| 32 |
* import and use the `logging module |
| 33 |
<https://docs.python.org/3/howto/logging.html>`_ directly if you |
| 34 |
don't have access to ``self`` |
| 35 |
* ``self.log`` is just a convenience wrapper for the standard |
| 36 |
Python ``logging`` module |
| 37 |
|
| 38 |
* send a message describing what the user did wrong and recommend a |
| 39 |
solution for them to try their command again |
| 40 |
* do not re-raise your exception in the ``except`` block as you |
| 41 |
normally would. This is usually done in order to produce an entry |
| 42 |
in the error logs, but we've already logged the exception, and by |
| 43 |
not re-raising it, we prevent that automatic "Computer says nooo. |
| 44 |
..." message from being sent |
| 45 |
|
| 46 |
Also, note that there is a ``errbot.ValidationException`` class which you |
| 47 |
can use inside your helper methods to raise meaningful errors and handle |
| 48 |
them accordingly. |
| 49 |
|
| 50 |
Here's an example: |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
from errbot import BotPlugin, arg_botcmd, ValidationException |
| 55 |
|
| 56 |
class FooBot(BotPlugin): |
| 57 |
"""An example bot""" |
| 58 |
|
| 59 |
@arg_botcmd('first_name', type=str) |
| 60 |
def add_first_name(self, message, first_name): |
| 61 |
"""Add your first name if it doesn't contain any digits""" |
| 62 |
try: |
| 63 |
FooBot.validate_first_name(first_name) |
| 64 |
except ValidationException as exc: |
| 65 |
self.log.exception( |
| 66 |
'first_name=%s contained a digit' % first_name |
| 67 |
) |
| 68 |
return 'Your first name cannot contain a digit.' |
| 69 |
|
| 70 |
# Add some code here to add the given name to your database |
| 71 |
|
| 72 |
return "Your name has been added." |
| 73 |
|
| 74 |
@staticmethod |
| 75 |
def validate_first_name(first_name): |
| 76 |
if any(char.isdigit() for char in first_name): |
| 77 |
raise ValidationException( |
| 78 |
"first_name=%s contained a digit" % first_name |
| 79 |
) |
| 80 |
|