njyjn / AttendanceBot

A Telegram bot which helps Caregroup Leaders in Arrow Ministry tabulate their group attendance.
MIT License
2 stars 3 forks source link

/event report returns NoneType error if no event exists in the database. #1

Open njyjn opened 7 years ago

njyjn commented 7 years ago

/event report

2017-02-05 01:44:18,802 : INFO     Received '/event report' from 'Justin' (87244565)
Traceback (most recent call last):
  File "/home/justin/.local/lib/python3.5/site-packages/telepot/delegate.py", line 256, in wait_loop
    j.on_message(msg)
  File "/home/justin/.local/lib/python3.5/site-packages/telepot/helper.py", line 630, in augmented
    return handler(msg)
  File "/home/justin/.local/lib/python3.5/site-packages/telepot/helper.py", line 996, in on_message
    self._router.route(msg)
  File "/home/justin/.local/lib/python3.5/site-packages/telepot/helper.py", line 972, in route
    return fn(msg, *args, **kwargs)
  File "/home/justin/.local/lib/python3.5/site-packages/telepot/helper.py", line 980, in <lambda>
    self._router = Router(flavor, {'chat': lambda msg: self.on_chat_message(msg),
  File "main.py", line 183, in on_chat_message
    reply(manager.printGrandTally())
  File "/home/justin/webprojects/AttendanceBot/manager.py", line 409, in printGrandTally
    total = getFinalString(tally.find_one( { } ))
  File "/home/justin/webprojects/AttendanceBot/manager.py", line 387, in getFinalString
    total = str(cgDoc['total'])
TypeError: 'NoneType' object is not subscriptable
2017-02-05 01:44:18,807 : ERROR    on_close() called due to TypeError: 'NoneType' object is not subscriptable

Add try-except block to handle error, and send user an appropriate message.

njyjn commented 7 years ago

This error is caused by the way the getFinalString() method is being overloaded. The way to fix this is to throw an error reply if no event is ongoing.

njyjn commented 7 years ago

Issue was fixed in headmaster.py with 9959b1d2038a38ce7113d856d5ca87f7f6258052 where cg['...'] was returning KeyError due to unpopulated field.

Fix: Use .get(..., 0) to enforce 0.

Now /event report returns an empty list which is a temporary fix. In the future, we need to pull up a 'no event error' or something.