Merge pull request 'better timeout handling' (#14) from dev into main
Some checks failed
Code Quality Check / quality-check (push) Successful in 13s
Safety Check / safety-check (push) Successful in 4m38s
Cleanup old Docker images / cleanup (push) Failing after 4m27s

Reviewed-on: #14
This commit is contained in:
wirehack7 2025-05-01 19:29:54 +02:00
commit 44d0315059

View File

@ -58,7 +58,7 @@ async def on_ready():
orientation="portrait or landscape (Standard: portrait)",
seed="Optional seed (integer). If empty, a random one will be generated",
)
# pylint: disable=too-many-locals,too-many-return-statements
# pylint: disable=too-many-locals,too-many-return-statements,too-many-statements
async def generate(
interaction: discord.Interaction,
prompt: str,
@ -159,16 +159,20 @@ async def generate(
}
start_time = time.monotonic()
async with aiohttp.ClientSession() as session:
async with session.post(
"https://image.novelai.net/ai/generate-image",
json=payload,
headers=headers,
timeout=aiohttp.ClientTimeout(total=120),
) as response:
response_bytes = await response.read()
status_code = response.status
try:
async with aiohttp.ClientSession() as session:
async with session.post(
"https://image.novelai.net/ai/generate-image",
json=payload,
headers=headers,
timeout=aiohttp.ClientTimeout(total=120),
) as response:
response_bytes = await response.read()
status_code = response.status
except aiohttp.TimeoutError:
await interaction.followup.send("Error: the request timed out.")
except aiohttp.ClientError as e:
await interaction.followup.send(f"Error: {e}")
duration = time.monotonic() - start_time
logger.info("Image creation took %.2f seconds", duration)
@ -214,9 +218,11 @@ async def generate(
except Exception: # pylint: disable=W0718
error_data = "error message unreadable"
error_message = f"Error {status_code} at API-request.\n" f"Answer: {error_data}\n"
error_message = (
f"Error {status_code} at API-request.\n" f"Answer: {error_data}\n"
)
await interaction.followup.send(error_message)
await interaction.followup.send(error_message)
bot.run(BOT_TOKEN)