Decorator error handling

This commit is contained in:
BarsTiger
2023-03-06 23:45:02 +02:00
parent a5ce82f148
commit ebdd27ece8
7 changed files with 104 additions and 137 deletions

View File

@@ -1,11 +1,11 @@
from aiogram import types
from bot.db import db, DBTables
from bot.utils.cooldown import throttle
from bot.keyboards.exception import get_exception_keyboard
from bot.utils.trace_exception import PrettyException
from bot.utils.errorable_command import wrap_exception
from bot.modules.get_hash.get_hash import get_hash
@wrap_exception([IndexError])
@throttle(cooldown=5, admin_ids=db[DBTables.config].get('admins'))
async def hash_command(message: types.Message):
try:
@@ -23,10 +23,3 @@ async def hash_command(message: types.Message):
except IndexError:
await message.reply('❌ Reply with this command on PICTURE OR FILE', parse_mode='html')
except Exception as e:
exception_id = f'{message.message_thread_id}-{message.message_id}'
db[DBTables.exceptions][exception_id] = PrettyException(e)
await message.reply('❌ Error happened while processing your request', parse_mode='html',
reply_markup=get_exception_keyboard(exception_id))
return

View File

@@ -1,11 +1,11 @@
from aiogram import types
from bot.db import db, DBTables
from bot.utils.cooldown import throttle
from bot.keyboards.exception import get_exception_keyboard
from bot.keyboards.image_info import get_img_info_keyboard
from bot.utils.trace_exception import PrettyException
from bot.utils.errorable_command import wrap_exception
@wrap_exception([IndexError])
@throttle(cooldown=10, admin_ids=db[DBTables.config].get('admins'))
async def imginfo(message: types.Message):
try:
@@ -25,10 +25,3 @@ async def imginfo(message: types.Message):
except IndexError:
await message.reply('❌ Reply with this command on PICTURE', parse_mode='html')
except Exception as e:
exception_id = f'{message.message_thread_id}-{message.message_id}'
db[DBTables.exceptions][exception_id] = PrettyException(e)
await message.reply('❌ Error happened while processing your request', parse_mode='html',
reply_markup=get_exception_keyboard(exception_id))
return

View File

@@ -2,36 +2,18 @@ from aiogram import types
from bot.db import db, DBTables
from bot.utils.cooldown import throttle
from bot.keyboards.set_model import get_set_model_keyboard
from bot.keyboards.exception import get_exception_keyboard
from bot.utils.trace_exception import PrettyException
from bot.modules.api.models import get_models
from aiohttp import ClientConnectorError
from bot.utils.errorable_command import wrap_exception
@wrap_exception()
@throttle(cooldown=60*60, admin_ids=db[DBTables.config].get('admins'), by_id=False)
async def set_model_command(message: types.Message):
temp_message = await message.reply('⏳ Getting models...')
try:
models = await get_models()
if models is not None and len(models) > 0:
db[DBTables.config]['models'] = models
else:
await temp_message.delete()
await message.reply('❌ No models available')
return
await temp_message.delete()
await message.reply("🪄 You can choose model from available:", reply_markup=get_set_model_keyboard(0))
except ClientConnectorError:
await message.reply('❌ Error! Maybe, StableDiffusion API endpoint is incorrect '
'or turned off')
await temp_message.delete()
models = await get_models()
if models is not None and len(models) > 0:
db[DBTables.config]['models'] = models
else:
await message.reply('❌ No models available')
return
except Exception as e:
exception_id = f'{message.message_thread_id}-{message.message_id}'
db[DBTables.exceptions][exception_id] = PrettyException(e)
await message.reply('❌ Error happened while processing your request', parse_mode='html',
reply_markup=get_exception_keyboard(exception_id))
return
await message.reply("🪄 You can choose model from available:", reply_markup=get_set_model_keyboard(0))

View File

@@ -2,44 +2,34 @@ from aiogram import types
from bot.db import db, DBTables
from bot.utils.cooldown import throttle
from bot.modules.api.objects.prompt_request import Prompt
from bot.keyboards.exception import get_exception_keyboard
from bot.utils.trace_exception import PrettyException
from bot.utils.errorable_command import wrap_exception
@wrap_exception(custom_loading=True)
async def _set_property(message: types.Message, prop: str, value=None):
temp_message = await message.reply(f"⏳ Setting {prop}...")
if not message.get_args():
await temp_message.edit_text("😶‍🌫️ Specify arguments for this command. Check /help")
return
try:
prompt: Prompt = db[DBTables.prompts].get(message.from_id)
if prompt is None and prop != 'prompt':
await temp_message.edit_text(f"You didn't created any prompt. Specify prompt text at least first time. "
f"For example, it can be: <code>masterpiece, best quality, 1girl, white hair, "
f"medium hair, cat ears, closed eyes, looking at viewer, :3, cute, scarf, "
f"jacket, outdoors, streets</code>", parse_mode='HTML')
return
elif prompt is None:
prompt = Prompt(message.get_args(), creator=message.from_id)
prompt.__setattr__(prop, message.get_args() if value is None else value)
prompt.creator = message.from_id
db[DBTables.prompts][message.from_id] = prompt
await db[DBTables.config].write()
await message.reply(f'{prop} set')
await temp_message.delete()
except Exception as e:
exception_id = f'{message.message_thread_id}-{message.message_id}'
db[DBTables.exceptions][exception_id] = PrettyException(e)
await message.reply('❌ Error happened while processing your request', parse_mode='html',
reply_markup=get_exception_keyboard(exception_id))
await temp_message.delete()
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1
prompt: Prompt = db[DBTables.prompts].get(message.from_id)
if prompt is None and prop != 'prompt':
await temp_message.edit_text(f"You didn't created any prompt. Specify prompt text at least first time. "
f"For example, it can be: <code>masterpiece, best quality, 1girl, white hair, "
f"medium hair, cat ears, closed eyes, looking at viewer, :3, cute, scarf, "
f"jacket, outdoors, streets</code>", parse_mode='HTML')
return
elif prompt is None:
prompt = Prompt(message.get_args(), creator=message.from_id)
prompt.__setattr__(prop, message.get_args() if value is None else value)
prompt.creator = message.from_id
db[DBTables.prompts][message.from_id] = prompt
await db[DBTables.config].write()
await message.reply(f'{prop} set')
await temp_message.delete()
@throttle(cooldown=5, admin_ids=db[DBTables.config].get('admins'))
@@ -125,30 +115,15 @@ async def set_restore_faces_command(message: types.Message):
await _set_property(message, 'restore_faces')
@wrap_exception()
@throttle(cooldown=5, admin_ids=db[DBTables.config].get('admins'))
async def set_sampler_command(message: types.Message):
temp_message = await message.reply('⏳ Getting samplers...')
from bot.modules.api.samplers import get_samplers
from aiohttp import ClientConnectorError
try:
if message.get_args() not in (samplers := await get_samplers()):
await message.reply(
f'❌ You can use only {", ".join(f"<code>{x}</code>" for x in samplers)}',
parse_mode='HTML'
)
return
await temp_message.delete()
except ClientConnectorError:
await message.reply('❌ Error! Maybe, StableDiffusion API endpoint is incorrect '
'or turned off')
await temp_message.delete()
return
except Exception as e:
exception_id = f'{message.message_thread_id}-{message.message_id}'
db[DBTables.exceptions][exception_id] = PrettyException(e)
await message.reply('❌ Error happened while processing your request', parse_mode='html',
reply_markup=get_exception_keyboard(exception_id))
await temp_message.delete()
if message.get_args() not in (samplers := await get_samplers()):
await message.reply(
f'❌ You can use only {", ".join(f"<code>{x}</code>" for x in samplers)}',
parse_mode='HTML'
)
return
await _set_property(message, 'sampler')

View File

@@ -6,12 +6,11 @@ from bot.modules.api.txt2img import txt2img
from bot.modules.api.objects.get_prompt import get_prompt
from bot.modules.api.objects.prompt_request import Generated
from bot.modules.api.status import wait_for_status
from bot.keyboards.exception import get_exception_keyboard
from bot.keyboards.image_info import get_img_info_keyboard
from bot.utils.trace_exception import PrettyException
from aiohttp import ClientConnectorError
from bot.utils.errorable_command import wrap_exception
@wrap_exception([ValueError], custom_loading=True)
@throttle(cooldown=30, admin_ids=db[DBTables.config].get('admins'))
async def generate_command(message: types.Message):
temp_message = await message.reply("⏳ Enqueued...")
@@ -33,10 +32,10 @@ async def generate_command(message: types.Message):
await wait_for_status()
await temp_message.edit_text(f"⌛ Generating...")
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1
image = await txt2img(prompt)
image_message = await message.reply_photo(photo=image[0])
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1
db[DBTables.generated][image_message.photo[0].file_unique_id] = Generated(
prompt=prompt,
seed=image[1]['seed'],
@@ -50,24 +49,8 @@ async def generate_command(message: types.Message):
await db[DBTables.config].write()
except ClientConnectorError:
await message.reply('❌ Error! Maybe, StableDiffusion API endpoint is incorrect '
'or turned off')
await temp_message.delete()
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1
return
except ValueError as e:
await message.reply(f'❌ Error! {e.args[0]}')
await temp_message.delete()
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1
return
except Exception as e:
exception_id = f'{message.message_thread_id}-{message.message_id}'
db[DBTables.exceptions][exception_id] = PrettyException(e)
await message.reply('❌ Error happened while processing your request', parse_mode='html',
reply_markup=get_exception_keyboard(exception_id))
await temp_message.delete()
db[DBTables.queue]['n'] = db[DBTables.queue].get('n', 1) - 1
return