Skip to content

Commit f261b84

Browse files
authored
Add extension errors from ext.commands.errors
1 parent 7c4b493 commit f261b84

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

discord/errors.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
'ConnectionClosed',
5353
'PrivilegedIntentsRequired',
5454
'InteractionResponded',
55+
'ExtensionError',
56+
'ExtensionAlreadyLoaded',
57+
'ExtensionNotLoaded',
58+
'NoEntryPointError',
59+
'ExtensionFailed',
60+
'ExtensionNotFound'
5561
)
5662

5763

@@ -275,3 +281,79 @@ class InteractionResponded(ClientException):
275281
def __init__(self, interaction: Interaction):
276282
self.interaction: Interaction = interaction
277283
super().__init__('This interaction has already been responded to before')
284+
285+
class ExtensionError(DiscordException):
286+
"""Base exception for extension related errors.
287+
288+
This inherits from :exc:`~discord.DiscordException`.
289+
290+
Attributes
291+
------------
292+
name: :class:`str`
293+
The extension that had an error.
294+
"""
295+
def __init__(self, message: Optional[str] = None, *args: Any, name: str) -> None:
296+
self.name: str = name
297+
message = message or f'Extension {name!r} had an error.'
298+
# clean-up @everyone and @here mentions
299+
m = message.replace('@everyone', '@\u200beveryone').replace('@here', '@\u200bhere')
300+
super().__init__(m, *args)
301+
302+
class ExtensionAlreadyLoaded(ExtensionError):
303+
"""An exception raised when an extension has already been loaded.
304+
305+
This inherits from :exc:`ExtensionError`
306+
"""
307+
def __init__(self, name: str) -> None:
308+
super().__init__(f'Extension {name!r} is already loaded.', name=name)
309+
310+
class ExtensionNotLoaded(ExtensionError):
311+
"""An exception raised when an extension was not loaded.
312+
313+
This inherits from :exc:`ExtensionError`
314+
"""
315+
def __init__(self, name: str) -> None:
316+
super().__init__(f'Extension {name!r} has not been loaded.', name=name)
317+
318+
class NoEntryPointError(ExtensionError):
319+
"""An exception raised when an extension does not have a ``setup`` entry point function.
320+
321+
This inherits from :exc:`ExtensionError`
322+
"""
323+
def __init__(self, name: str) -> None:
324+
super().__init__(f"Extension {name!r} has no 'setup' function.", name=name)
325+
326+
class ExtensionFailed(ExtensionError):
327+
"""An exception raised when an extension failed to load during execution of the module or ``setup`` entry point.
328+
329+
This inherits from :exc:`ExtensionError`
330+
331+
Attributes
332+
-----------
333+
name: :class:`str`
334+
The extension that had the error.
335+
original: :exc:`Exception`
336+
The original exception that was raised. You can also get this via
337+
the ``__cause__`` attribute.
338+
"""
339+
def __init__(self, name: str, original: Exception) -> None:
340+
self.original: Exception = original
341+
msg = f'Extension {name!r} raised an error: {original.__class__.__name__}: {original}'
342+
super().__init__(msg, name=name)
343+
344+
class ExtensionNotFound(ExtensionError):
345+
"""An exception raised when an extension is not found.
346+
347+
This inherits from :exc:`ExtensionError`
348+
349+
.. versionchanged:: 1.3
350+
Made the ``original`` attribute always None.
351+
352+
Attributes
353+
-----------
354+
name: :class:`str`
355+
The extension that had the error.
356+
"""
357+
def __init__(self, name: str) -> None:
358+
msg = f'Extension {name!r} could not be loaded.'
359+
super().__init__(msg, name=name)

0 commit comments

Comments
 (0)