diff --git a/readthedocs/extra/basic/working-with-updates.rst b/readthedocs/extra/basic/working-with-updates.rst index ab2e5d11..970447bd 100644 --- a/readthedocs/extra/basic/working-with-updates.rst +++ b/readthedocs/extra/basic/working-with-updates.rst @@ -131,18 +131,21 @@ propagation of the update through your handlers to stop: .. code-block:: python + from telethon.events import StopPropagation + @client.on(events.NewMessage) def _(event): # ... some conditions event.delete() # Other handlers won't have an event to work with - raise client.StopPropagation + raise StopPropagation @client.on(events.NewMessage) def _(event): - pass # Will never be reached, because - # it is the second handler in the chain. + # Will never be reached, because it is the second handler + # in the chain. + pass Events module diff --git a/telethon/events/__init__.py b/telethon/events/__init__.py index 48b26004..c5c87fbd 100644 --- a/telethon/events/__init__.py +++ b/telethon/events/__init__.py @@ -873,3 +873,26 @@ class MessageChanged(_EventBuilder): self.edited = bool(edit_msg) self.deleted = bool(deleted_ids) self.deleted_ids = deleted_ids or [] + + +class StopPropagation(Exception): + """ + If this Exception is found to be raised in any of the handlers for a + given update, it will stop the execution of all other registered + event handlers in the chain. + Think of it like a ``StopIteration`` exception in a for loop. + + Example usage: + ``` + @client.on(events.NewMessage) + def delete(event): + event.delete() + # Other handlers won't have an event to work with + raise StopPropagation + + @client.on(events.NewMessage) + def _(event): + # Will never be reached, because it is the second handler in the chain. + pass + ``` + """ diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 1e1d80e2..beaf71d7 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -1887,9 +1887,10 @@ class TelegramClient(TelegramBareClient): event._client = self try: callback(event) - except StopPropagation: - __log__.info("Event handler '{}' stopped chain of propagation for update {}.".format( - callback.__name__, type(update).__name__)) + except events.StopPropagation: + __log__.debug("Event handler '{}' stopped chain of " + "propagation for update {}.".format( + callback.__name__, type(update).__name__)) break def add_event_handler(self, callback, event=None): @@ -2130,30 +2131,3 @@ class TelegramClient(TelegramBareClient): ) # endregion - - -# region Exceptions - -class StopPropagation(Exception): - """ - If this Exception is found to be raised in any of the handlers for a - given update, it will stop the execution of all other registered - event handlers in the chain. - Think of it like a ``StopIteration`` exception in a for loop. - - Example usage: - ``` - @client.on(events.NewMessage) - def delete(event): - event.delete() - # Other handlers won't have an event to work with - raise StopPropagation - - @client.on(events.NewMessage) - def _(event): - pass # Will never be reached, because - # it is the second handler in the chain. - ``` - """ - -# endregion