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 1 commit
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
6 changes: 6 additions & 0 deletions bottery/bottery.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ def configure_server(self, port):
port=port,
))

def exception_handler(self, loop, context):
click.echo(context.get('exception'))
loop.default_exception_handler(context)
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
7 changes: 7 additions & 0 deletions bottery/telegram/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ async def polling(self, last_update=None):

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

if not updates.get('ok'):
msg = '[{}] Unable to receive updates, data={}, error'.format(
self.platform,
updates
)
raise Exception(msg)
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 create a specific exception? Something like PlatformError? What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

Sure, I'll change an Exception to something more meaningful 😄


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