From 1f3aa377c37e445a659d80b1a90c30b9854b9e67 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 22 Mar 2022 19:07:22 -0400 Subject: [PATCH] Wrap non-AppCommandError exceptions with TransformerError --- discord/app_commands/errors.py | 9 +++++---- discord/app_commands/transformers.py | 10 ++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/discord/app_commands/errors.py b/discord/app_commands/errors.py index 4bcbcc98a..d6e8ddc27 100644 --- a/discord/app_commands/errors.py +++ b/discord/app_commands/errors.py @@ -92,10 +92,11 @@ class TransformerError(AppCommandError): This inherits from :exc:`~discord.app_commands.AppCommandError`. - .. note:: - - If the transformer raises a custom :exc:`AppCommandError` then it will - be propagated rather than wrapped into this exception. + If an exception occurs while converting that does not subclass + :exc:`AppCommandError` then the exception is wrapped into this exception. + The original exception can be retrieved using the ``__cause__`` attribute. + Otherwise if the exception derives from :exc:`AppCommandError` then it will + be propagated as-is. .. versionadded:: 2.0 diff --git a/discord/app_commands/transformers.py b/discord/app_commands/transformers.py index d10754092..2078e8ec1 100644 --- a/discord/app_commands/transformers.py +++ b/discord/app_commands/transformers.py @@ -44,7 +44,7 @@ from typing import ( Union, ) -from .errors import TransformerError +from .errors import AppCommandError, TransformerError from .models import AppCommandChannel, AppCommandThread, Choice from ..channel import StageChannel, StoreChannel, VoiceChannel, TextChannel, CategoryChannel from ..enums import AppCommandOptionType, ChannelType @@ -136,7 +136,13 @@ class CommandParameter: raise TransformerError(value, self.type, self._annotation) return choice - return await self._annotation.transform(interaction, value) + try: + return await self._annotation.transform(interaction, value) + except AppCommandError: + raise + except Exception as e: + raise TransformerError(value, self.type, self._annotation) from e + return value