Skip to content

Commit 7a1a8cd

Browse files
committed
Add Translator Error
1 parent f9466f8 commit 7a1a8cd

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

docs/exts/commands/exceptions.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Exceptions
5757

5858
.. autoexception:: twitchio.ext.commands.NoEntryPointError
5959

60+
.. autoexception:: twitchio.ext.commands.TranslatorError
61+
6062

6163
Exception Hierarchy
6264
~~~~~~~~~~~~~~~~~~~
@@ -80,6 +82,7 @@ Exception Hierarchy
8082
- :exc:`ExpectedClosingQuoteError`
8183
- :exc:`GuardFailure`
8284
- :exc:`CommandOnCooldown`
85+
- :exc:`TranslatorError`
8386
- :exc:`ModuleError`
8487
- :exc:`ModuleLoadFailure`
8588
- :exc:`ModuleAlreadyLoadedError`

twitchio/ext/commands/context.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ async def send_translated(self, content: str, *, me: bool = False, langcode: str
591591
Twitch failed to process the message, could be ``400``, ``401``, ``403``, ``422`` or any ``5xx`` status code.
592592
MessageRejectedError
593593
Twitch rejected the message from various checks.
594+
TranslatorError
595+
An error occurred during translation.
594596
"""
595597
translator: Translator | None = getattr(self.command, "translator", None)
596598
new = (f"/me {content}" if me else content).strip()
@@ -599,14 +601,21 @@ async def send_translated(self, content: str, *, me: bool = False, langcode: str
599601
return await self.channel.send_message(sender=self.bot.bot_id, message=new)
600602

601603
invoked = self.invoked_with
602-
code = langcode or translator.get_langcode(self, invoked.lower()) if invoked else None
604+
605+
try:
606+
code = langcode or translator.get_langcode(self, invoked.lower()) if invoked else None
607+
except Exception as e:
608+
raise TranslatorError(f"An exception occurred fetching a language code for '{invoked}'.", original=e) from e
603609

604610
if not code:
605611
return await self.channel.send_message(sender=self.bot.bot_id, message=new)
606612

607-
translated = await translator.translate(self, content, code)
608-
new_translated = (f"/me {translated}" if me else translated).strip()
613+
try:
614+
translated = await translator.translate(self, content, code)
615+
except Exception as e:
616+
raise TranslatorError(f"An exception occurred translating content for '{invoked}'.", original=e) from e
609617

618+
new_translated = (f"/me {translated}" if me else translated).strip()
610619
return await self.channel.send_message(sender=self.bot.bot_id, message=new_translated)
611620

612621
async def reply(self, content: str, *, me: bool = False) -> SentMessage:

twitchio/ext/commands/exceptions.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"ModuleNotLoadedError",
5252
"NoEntryPointError",
5353
"PrefixError",
54+
"TranslatorError",
5455
"UnexpectedQuoteError",
5556
)
5657

@@ -72,7 +73,7 @@ class CommandInvokeError(CommandError):
7273
Attributes
7374
----------
7475
original: :class:`Exception` | None
75-
The original exception that caused this error. Could be None.
76+
The original exception that caused this error. Could be ``None``.
7677
"""
7778

7879
def __init__(self, msg: str | None = None, original: Exception | None = None) -> None:
@@ -204,3 +205,18 @@ def __init__(self, msg: str | None = None, *, cooldown: BaseCooldown, remaining:
204205
self.cooldown: BaseCooldown = cooldown
205206
self.remaining: float = remaining
206207
super().__init__(msg or f"Cooldown is ratelimited. Try again in {remaining} seconds.")
208+
209+
210+
class TranslatorError(CommandError):
211+
"""Exception raised when a :class:`.commands.Translator` raises an error while attempting to get a language code
212+
or translate content provided to :meth:`~.commands.Context.send_translated`.
213+
214+
Attributes
215+
----------
216+
original: :class:`Exception` | None
217+
The original exception that caused this error. Could be None.
218+
"""
219+
220+
def __init__(self, msg: str | None = None, original: Exception | None = None) -> None:
221+
self.original: Exception | None = original
222+
super().__init__(msg)

0 commit comments

Comments
 (0)