Skip to content

Commit

Permalink
cleaned up errors.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nfearnley committed May 21, 2024
1 parent 08416de commit ef4cc68
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 60 deletions.
12 changes: 6 additions & 6 deletions sizebot/extensions/errorhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ async def on_command_error(ctx: BotContext, error: commands.CommandError):
err = errors.MultilineAsNonFirstCommandException()

if isinstance(err, errors.UserMessedUpException):
await ctx.send(f"{emojis.error} {err.formatUserMessage(ctx)}")
await ctx.send(f"{emojis.error} {err.format_user_message(ctx)}")

if isinstance(err, errors.DigiContextException):
# DigiContextException handling
message = await err.formatMessage(ctx)
message = await err.format_message(ctx)
if message is not None:
logger.log(err.level, message)
logger.error(utils.format_traceback(error))
userMessage = await err.formatUserMessage(ctx)
userMessage = await err.format_user_message(ctx)
if userMessage is not None:
await ctx.send(f"{emojis.warning} {userMessage}")
elif isinstance(err, errors.DigiException):
# DigiException handling
message = err.formatMessage()
message = err.format_message()
if message is not None:
logger.log(err.level, message)
logger.error(utils.format_traceback(error))
userMessage = err.formatUserMessage()
userMessage = err.format_user_message()
if userMessage is not None:
await ctx.send(f"{emojis.warning} {userMessage}")
elif isinstance(err, commands.errors.MissingRequiredArgument):
Expand Down Expand Up @@ -82,7 +82,7 @@ async def on_error(event: discord.DiscordException, *args, **kwargs):
err = getattr(error, "original", error)
# DigiException handling
if isinstance(err, errors.DigiException):
message = err.formatMessage()
message = err.format_message()
if message is not None:
logger.log(err.level, message)
logger.error(utils.format_traceback(error))
Expand Down
91 changes: 38 additions & 53 deletions sizebot/lib/errors.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
from typing import Any

import importlib.resources as pkg_resources
import logging
import json

import sizebot.data
from sizebot.conf import conf
from sizebot.lib import utils
from sizebot.lib import macrovision, utils
from sizebot.lib.types import BotContext

modelJSON = json.loads(pkg_resources.read_text(sizebot.data, "models.json"))
logger = logging.getLogger("sizebot")


# error.message will be printed when you do print(error)
# error.user_message will be displayed to the user
# error.format_message will be printed when you do print(error)
# error.format_user_message will be displayed to the user
class DigiException(Exception):
level = logging.WARNING

# TODO: CamelCase
def formatMessage(self) -> str | None:
def format_message(self) -> str | None:
return None

# TODO: CamelCase
def formatUserMessage(self) -> str | None:
def format_user_message(self) -> str | None:
return None

def __repr__(self) -> str:
return utils.get_fullname(self)

def __str__(self) -> str:
return self.formatMessage() or self.formatUserMessage() or repr(self)
return self.format_message() or self.format_user_message() or repr(self)


class DigiContextException(Exception):
level = logging.WARNING

# TODO: CamelCase
async def formatMessage(self, ctx: BotContext) -> str | None:
async def format_message(self, ctx: BotContext) -> str | None:
return None

# TODO: CamelCase
async def formatUserMessage(self, ctx: BotContext) -> str | None:
async def format_user_message(self, ctx: BotContext) -> str | None:
return None

def __repr__(self) -> str:
Expand All @@ -58,7 +52,7 @@ def __init__(self, guildid: int, userid: int, unreg: bool = False):
self.unreg = unreg

# TODO: CamelCase
async def formatUserMessage(self, ctx: BotContext) -> str:
async def format_user_message(self, ctx: BotContext) -> str:
user = await ctx.guild.fetch_member(self.userid)
usernick = user.display_name
returnstr = f"Sorry, {usernick} isn't registered with SizeBot."
Expand All @@ -74,29 +68,29 @@ def __init__(self, guildid: int):
self.guildid = guildid

# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return f"Guild {self.guildid} not found."


class ValueIsZeroException(DigiException):
# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return "Value zero received when unexpected."

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return (
"Nice try.\n"
"You can't change by a value of zero.")


class ValueIsOneException(DigiException):
# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return "Value one received when unexpected."

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return (
"Nice try.\n"
"You can't change by a value of one.\n"
Expand All @@ -110,7 +104,7 @@ def __init__(self, changemethod: str):
self.changemethod = changemethod

# TODO: CamelCase
async def formatUserMessage(self, ctx: BotContext) -> str:
async def format_user_message(self, ctx: BotContext) -> str:
usernick = ctx.author.display_name
return f"Sorry, {usernick}! {self.changemethod} is not a valid change method."

Expand All @@ -119,26 +113,17 @@ class CannotSaveWithoutIDException(DigiException):
level = logging.CRITICAL

# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return "Tried to save a user without an ID."


class InvalidUnitSystemException(DigiException):
def __init__(self, unitsystem: str):
self.unitsystem = unitsystem

# TODO: CamelCase
def formatUserMessage(self) -> str:
return f"{self.unitsystem!r} is an unrecognized unit system."


class InvalidSizeValue(DigiException):
def __init__(self, sizevalue: str, kind: str):
self.sizevalue = sizevalue
self.kind = kind

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return f"{self.sizevalue!r} is an unrecognized {self.kind} value."


Expand All @@ -147,7 +132,7 @@ def __init__(self, value: str):
self.value = value

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return f"{self.value!r} is an unrecognized stat."


Expand All @@ -156,7 +141,7 @@ def __init__(self, value: str):
self.value = value

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return f"{self.value!r} is an unrecognized stat tag."


Expand All @@ -165,7 +150,7 @@ def __init__(self, name: str):
self.name = name

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return f"{self.name!r} is an unrecognized object."


Expand All @@ -174,11 +159,11 @@ def __init__(self, name: str):
self.name = name

# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return f"{self.name!r} is an unrecognized Macrovision model."

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return f"{self.name!r} is an unrecognized Macrovision model."


Expand All @@ -187,15 +172,15 @@ def __init__(self, model: str, view: str):
self.model = model
self.view = view

if self.model not in modelJSON.keys():
if not macrovision.is_model(model):
raise InvalidMacrovisionModelException(self.model)

# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return f"{self.view!r} is an unrecognized view for the Macrovision model {self.model!r}."

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return f"{self.view!r} is an unrecognized view for the Macrovision model {self.model!r}."


Expand All @@ -204,36 +189,36 @@ def __init__(self, dString: str):
self.dString = dString

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return f"Invalid roll string `{self.dString}`."


class AdminPermissionException(DigiContextException):
# TODO: CamelCase
async def formatMessage(self, ctx: BotContext) -> str:
async def format_message(self, ctx: BotContext) -> str:
usernick = ctx.author.display_name
return f"{usernick} tried to run an admin command."

# TODO: CamelCase
async def formatUserMessage(self, ctx: BotContext) -> str:
async def format_user_message(self, ctx: BotContext) -> str:
usernick = ctx.author.display_name
return f"{usernick} tried to run an admin command. This incident will be reported."


class MultilineAsNonFirstCommandException(DigiContextException):
# TODO: CamelCase
async def formatMessage(self, ctx: BotContext) -> str:
async def format_message(self, ctx: BotContext) -> str:
usernick = ctx.author.display_name
return f"{usernick} tried to run a multi-line command in the middle of a sequence."

# TODO: CamelCase
async def formatUserMessage(self, ctx: BotContext) -> str:
async def format_user_message(self, ctx: BotContext) -> str:
return "You are unable to run a command that takes a multi-line argument in the middle of a batch command sequence. Please try running these commands seperately."


class ArgumentException(DigiContextException):
# TODO: CamelCase
async def formatUserMessage(self, ctx: BotContext) -> str:
async def format_user_message(self, ctx: BotContext) -> str:
return f"Please enter `{ctx.prefix}{ctx.invoked_with} {ctx.command.signature}`."


Expand All @@ -242,7 +227,7 @@ def __init__(self, custommessage: str):
self.custommessage = custommessage

# TODO: CamelCase
async def formatUserMessage(self, ctx: BotContext) -> str:
async def format_user_message(self, ctx: BotContext) -> str:
return self.custommessage


Expand All @@ -253,11 +238,11 @@ def __init__(self, custommessage: str):
self.custommessage = custommessage

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return "This should never happen. Something very wrong has occured."

# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return self.custommessage


Expand All @@ -267,11 +252,11 @@ def __init__(self, s: str, t: str):
self.t = t

# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return f"Could not parse {self.s} into a {self.t}."

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return f"Could not parse {self.s} into a {self.t}."


Expand All @@ -280,9 +265,9 @@ def __init__(self, s: list[Any]):
self.s = utils.sentence_join(getattr(t, "key", repr(t)) for t in s)

# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return f"Could not calculate the {self.s} stat(s)."

# TODO: CamelCase
def formatUserMessage(self) -> str:
def format_user_message(self) -> str:
return f"Could not calculate the {self.s} stat(s)."
2 changes: 1 addition & 1 deletion sizebot/lib/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class TooManyMenuOptionsException(DigiException):
# TODO: CamelCase
def formatMessage(self) -> str:
def format_message(self) -> str:
return "Too many options for this reaction menu. (limit is 20.)"


Expand Down

0 comments on commit ef4cc68

Please sign in to comment.