better timeout handling
Some checks failed
Safety Check / safety-check (pull_request) Successful in -1s
Code Quality Check / quality-check (pull_request) Failing after 14s

This commit is contained in:
wirehack7 2025-05-01 19:25:48 +02:00
parent 6bf61fdbf0
commit 90c97d3f54
Signed by: wirehack7
GPG Key ID: C9F4CF85BA505ADC

View File

@ -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,9 @@ 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)