|
37 | 37 | from .custom_embeds import ErrorEmbed, SimpleEmbed, SuccessEmbed |
38 | 38 | from .problems_module.errors import ( |
39 | 39 | LinearAlgebraUserInputErrorException, |
40 | | - LockedCacheException, |
| 40 | + LockedCacheException |
41 | 41 | ) |
| 42 | +from error_handler.handle_known_errors import handle_known_error |
| 43 | +from error_handler.handle_unexpected_error import handle_unexpected_error |
42 | 44 | from .the_documentation_file_loader import DocumentationFileLoader |
43 | 45 |
|
44 | 46 |
|
45 | | -def get_git_revision_hash() -> str: |
46 | | - """A method that gets the git revision hash. Credit to https://stackoverflow.com/a/21901260 for the code :-)""" |
47 | | - return subprocess.check_output( |
48 | | - ["git", "rev-parse", "HEAD"], encoding="ascii", errors="ignore" |
49 | | - ).strip()[ |
50 | | - :7 |
51 | | - ] # [7:] is here because of the commit hash, the rest of this function is from stack overflow |
| 47 | + |
| 48 | + |
| 49 | + |
52 | 50 |
|
53 | 51 |
|
54 | 52 | async def base_on_error( |
55 | 53 | inter: disnake.ApplicationCommandInteraction, error: BaseException | Exception |
56 | 54 | ): |
57 | 55 | """The base on_error event. Call this and use the dictionary as keyword arguments to print to the user""" |
58 | | - print("OH NO AN ERROR OCCURED!!!!") |
| 56 | + print("OH NO AN ERROR OCCURRED!!!!") |
59 | 57 | if isinstance(error, BaseException) and not isinstance(error, Exception): |
60 | 58 | # Errors that do not inherit from Exception are not meant to be caught |
61 | 59 | await inter.bot.close() |
62 | 60 | if exc_info()[0] is not None: |
63 | 61 | raise |
64 | 62 | raise error |
65 | | - cause = None |
66 | | - if error.__context__ is not None: |
67 | | - cause = error.__context__ |
68 | | - print(cause, error, inter) |
69 | | - if isinstance(error, LockedCacheException): |
70 | | - return { |
71 | | - "content": "The bot's cache's lock is currently being held. Please try again later." |
72 | | - } |
73 | | - # print(isinstance(cause, LinearAlgebraUserInputErrorException)) |
74 | | - # print(type(cause)) |
75 | | - if isinstance(error, LinearAlgebraUserInputErrorException): |
76 | | - return {"embed": ErrorEmbed(str(cause))} |
77 | | - if isinstance(error, (OnCooldown, disnake.ext.commands.CommandOnCooldown)): |
78 | | - # This is a cooldown exception |
79 | | - content = f"This command is on cooldown; please retry **{disnake.utils.format_dt(disnake.utils.utcnow() + datetime.timedelta(seconds=error.retry_after), style='R')}**." |
80 | | - return {"content": content, "delete_after": error.retry_after} |
81 | | - if isinstance(error, (disnake.Forbidden,)): |
82 | | - extra_content = """There was a 403 error. This means either |
83 | | - 1) You didn't give me enough permissions to function correctly, or |
84 | | - 2) There's a bug! If so, please report it! |
85 | | -
|
86 | | - The error traceback is below.""" |
87 | | - error_traceback = "\n".join(traceback.format_exception(error)) |
88 | | - paginator = PaginatorView.paginate( |
89 | | - user_id=inter.author.id, |
90 | | - text=extra_content + error_traceback, |
91 | | - breaking_chars="\n", |
92 | | - max_page_length=1900, |
93 | | - special_color=disnake.Color.red(), |
94 | | - ) |
95 | | - return {"embed": paginator.create_embed(), "view": paginator} |
96 | | - |
97 | | - if isinstance(error, commands.NotOwner): |
98 | | - return {"embed": ErrorEmbed("You are not the owner of this bot.")} |
99 | | - if isinstance(error, disnake.ext.commands.errors.CheckFailure): |
100 | | - return {"embed": ErrorEmbed(str(error))} |
101 | | - |
102 | | - # Embed = ErrorEmbed(custom_title="⚠ Oh no! Error: " + str(type(error)), description=("Command raised an exception:" + str(error))) |
103 | | - logging.error("Uh oh - an error occurred ", exc_info=exc_info()) |
104 | | - error_traceback = "\n".join(traceback.format_exception(error)) |
105 | | - print( |
106 | | - "\n".join(traceback.format_exception(error)), # python 3.10 only! |
107 | | - file=stderr, |
108 | | - ) |
109 | | - |
110 | | - error_msg = """An error occurred! |
111 | | -
|
112 | | - Steps you should do: |
113 | | - 1) Please report this bug to me! (Either create a github issue, or report it in the support server) |
114 | | - 2) If you are a programmer, please suggest a fix by creating a Pull Request. |
115 | | - 3) Please don't use this command until it gets fixed in a later update! |
116 | | -
|
117 | | - The error traceback is shown below; this may be removed/DMed to the user in the future. |
118 | | -
|
119 | | - """ # TODO: update when my support server becomes public & think about providing the traceback to the user |
120 | | - traceback_msg = disnake.utils.escape_markdown(error_traceback) |
121 | | - additional_error = "" |
122 | | - try: |
123 | | - await log_error(error) # Log the error |
124 | | - except Exception as log_error_exc: |
125 | | - additional_error = ( |
126 | | - """Additionally, while trying to log this error, the following exception occurred: \n""" |
127 | | - + disnake.utils.escape_markdown( |
128 | | - "\n".join(traceback.format_exception(log_error_exc)) |
129 | | - ) |
130 | | - ) |
131 | | - |
132 | | - try: |
133 | | - embed = disnake.Embed( |
134 | | - colour=disnake.Colour.red(), |
135 | | - description=error_msg + traceback_msg + additional_error, |
136 | | - title="Oh, no! An error occurred!", |
137 | | - ) |
138 | | - except (TypeError, NameError) as e: |
139 | | - # send as plain text |
140 | | - plain_text = ( |
141 | | - """Oh no! An Exception occurred! And it couldn't be sent as an embed!```""" |
142 | | - ) |
143 | | - plain_text += error_msg + traceback_msg + additional_error |
144 | | - plain_text += f"```Time: {str(asctime())} Commit hash: {get_git_revision_hash()} The stack trace is shown for debugging purposes. The stack trace is also logged (and pushed), but should not contain identifying information (only code which is on github)" |
145 | | - |
146 | | - plain_text += f"Error that occurred while attempting to send it as an embed:" |
147 | | - plain_text += disnake.utils.escape_markdown( |
148 | | - "".join(traceback.format_exception(e)) |
149 | | - )[: -(1650 - len(plain_text))] |
150 | | - the_new_exception = deepcopy(e) |
151 | | - the_new_exception.__cause__ = error |
152 | | - if len(plain_text) > 2000: |
153 | | - # uh oh |
154 | | - raise RuntimeError( |
155 | | - "An error occurred; could not send it as an embed nor as plain text!" |
156 | | - ) from the_new_exception |
157 | | - |
158 | | - return {"content": plain_text} |
159 | | - footer = f"Time: {str(asctime())} Commit hash: {get_git_revision_hash()} The stack trace is shown for debugging purposes. The stack trace is also logged (and pushed), but should not contain identifying information (only code which is on github)" |
160 | | - embed.set_footer(text=footer) |
161 | | - if len(embed.description) < 2048: |
162 | | - return {"embed": embed} |
163 | | - paginator = PaginatorView.paginate( |
164 | | - user_id=inter.author.id, |
165 | | - text=error_msg, |
166 | | - breaking_chars="\n", |
167 | | - max_page_length=1900, |
168 | | - special_color=disnake.Color.red(), |
169 | | - ) |
170 | | - paginator.add_pages( |
171 | | - PaginatorView.break_into_pages( |
172 | | - traceback_msg, max_page_length=1900, breaking_chars="\n" |
173 | | - ) |
174 | | - ) |
175 | | - if additional_error: |
176 | | - paginator.add_pages( |
177 | | - PaginatorView.break_into_pages( |
178 | | - additional_error, max_page_length=1900, breaking_chars="\n" |
179 | | - ) |
180 | | - ) |
181 | | - accounted_for = len(error_msg) + len(traceback_msg) + len(additional_error) |
182 | | - if len(embed.description) != accounted_for: |
183 | | - paginator.add_pages( |
184 | | - PaginatorView.break_into_pages( |
185 | | - embed.description[accounted_for:], |
186 | | - max_page_length=1900, |
187 | | - breaking_chars="\n", |
188 | | - ) |
189 | | - ) |
190 | | - first_page = paginator.create_embed() |
191 | | - return {"embed": first_page, "view": paginator} |
| 63 | + known_error_result = handle_known_error(error) |
| 64 | + if known_error_result is not None: |
| 65 | + return known_error_result |
| 66 | + return handle_unexpected_error(error) |
| 67 | + |
0 commit comments