better timeout handling #14
28
src/main.py
28
src/main.py
@ -159,16 +159,20 @@ async def generate(
|
|||||||
}
|
}
|
||||||
|
|
||||||
start_time = time.monotonic()
|
start_time = time.monotonic()
|
||||||
|
try:
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(
|
async with session.post(
|
||||||
"https://image.novelai.net/ai/generate-image",
|
"https://image.novelai.net/ai/generate-image",
|
||||||
json=payload,
|
json=payload,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
timeout=aiohttp.ClientTimeout(total=120),
|
timeout=aiohttp.ClientTimeout(total=120),
|
||||||
) as response:
|
) as response:
|
||||||
response_bytes = await response.read()
|
response_bytes = await response.read()
|
||||||
status_code = response.status
|
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
|
duration = time.monotonic() - start_time
|
||||||
logger.info("Image creation took %.2f seconds", duration)
|
logger.info("Image creation took %.2f seconds", duration)
|
||||||
@ -214,9 +218,9 @@ async def generate(
|
|||||||
except Exception: # pylint: disable=W0718
|
except Exception: # pylint: disable=W0718
|
||||||
error_data = "error message unreadable"
|
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)
|
bot.run(BOT_TOKEN)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user