Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#179: Added general telegram exceptions handling. #180

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ _Changes to be released_
#### Bug fixes

- Stop responding with `null` when a view doesn't return anything ([#154](https://github.com/rougeth/bottery/issues/154))
- Stop the loop when bot is unable to retrieve updates from Telegram ([#179](https://github.com/rougeth/bottery/issues/179))

#### Docs

Expand Down
8 changes: 8 additions & 0 deletions bottery/bottery.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,16 @@ def configure_server(self, port):
port=port,
))

def exception_handler(self, loop, context):
click.echo(context.get('exception'))
loop.default_exception_handler(context)
# We don't use Bottery.stop() here because the current implementation
# won't stop the running loop.
loop.stop()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call self.stop() here?

Copy link
Author

@ivkalita ivkalita Oct 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, calling self.stop() here will cause an exception "cannot close running event loop". Also, these two methods are semantically different: self.loop.close() (inside self.stop()) tries to close event loop immediately (which is not possible in exception handler), but loop.stop() marks loop to be closed ASAP. I was thinking about replacing self.loop.close() by self.loop.stop(), but I'm not sure if this issue a good place for such a proposal 😄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think self.stop wasn't done the right way. There's another issue related to it (#175). So yep, this PR isn't the best place to do discuss it indeed, but I think we should have at least a commend here saying that the stop/close should be done by another self.stop().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so I'll add a comment in this PR, and also will try to fill a PR for the #175 to stop the event loop gracefully.


def configure(self):
self.loop.run_until_complete(self.configure_platforms())
self.loop.set_exception_handler(self.exception_handler)

def run(self, server_port):
click.echo('{now}\n{bottery} version {version}'.format(
Expand Down
10 changes: 10 additions & 0 deletions bottery/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ class ValidationError(Exception):

class BotteryDeprecationWarning(Warning):
pass


class PlatformError(Exception):
def __init__(self, platform, message):
super().__init__()
self.platform = platform
self.message = message

def __str__(self):
return '[{}] {}'.format(self.platform, self.message)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started questioning myself if the [{}] {} is a pattern that we should keep following. In this case, I think we don't need the self.platform, the exception itself should be able to give all the info we need. Maybe class TelegramException(PlatformException) is a good idea. What do you think?

7 changes: 7 additions & 0 deletions bottery/telegram/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from aiohttp import web

from bottery.conf import settings
from bottery.exceptions import PlatformError
from bottery.message import Message
from bottery.platforms import BaseEngine
from bottery.telegram import TelegramAPI
Expand Down Expand Up @@ -85,6 +86,12 @@ async def polling(self, last_update=None):

updates = await self.api.get_updates(**payload)

if not updates.get('ok'):
raise PlatformError(
self.platform,
'Unable to receive updates, data={}'.format(updates)
)

# If polling request returned at least one update, use its ID
# to define the offset.
if len(updates.get('result', [])):
Expand Down