-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCustomExceptions.py
More file actions
62 lines (53 loc) · 2.43 KB
/
Copy pathCustomExceptions.py
File metadata and controls
62 lines (53 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class BaseCustomException(Exception):
"""
An abstract class that other custom exceptions should inherit from. Don't instantiate this directly
"""
def __init__(self, displayMessage):
"""
Create a new exception
:param displayMessage: An optional user-facing message, that will also be used as the exception's string representation
"""
super(BaseCustomException, self).__init__(displayMessage)
self.displayMessage = displayMessage
def __str__(self):
if self.displayMessage:
return self.displayMessage
return super(BaseCustomException, self).__str__()
def __repr__(self):
if self.displayMessage:
return "<{}> {}".format(self.__class__.__name__, self.displayMessage)
return super(BaseCustomException, self).__repr__()
class CommandException(BaseCustomException):
"""
This custom exception can be thrown by commands when something goes wrong during execution.
The parameter is a message sent to the source that called the command (a channel or a user)
"""
def __init__(self, displayMessage=None, shouldLogError=True):
"""
Create a new CommandException, to be thrown when something goes wrong during Command execution
:param displayMessage: An optional message to display to the IRC chat the bot is in
:param shouldLogError: Whether this exception should be logged to the program log. This is useful if it's a problem that needs to be solved, but can be set to False if it's a user input error
"""
super(CommandException, self).__init__(displayMessage)
self.shouldLogError = shouldLogError
class CommandInputException(CommandException):
"""
This custom exception can be raised when the input to some module or command is invalid or can't be parsed.
It is a more specific implementation of the CommandException, that doesn't log itself to the logfile
"""
def __init__(self, displayMessage):
"""
Create a new InputException. The display message will be shown to the user
:param displayMessage: The message to show to the user that called the command. This message should explain how the input should be correctly formatted
"""
super(CommandInputException, self).__init__(displayMessage, False)
class SettingException(BaseCustomException):
"""
This custom exception gets thrown when something is wrong with a bot setting
"""
pass
class WebRequestException(BaseCustomException):
"""
This custom exception gets thrown when a web request goes wrong, either through timeout, a missing API key, or something else
"""
pass