message d'erreur personnalisable
This commit is contained in:
34
callbacks.py
34
callbacks.py
@@ -6,23 +6,25 @@ from matrix_bot.config import logger
|
||||
from matrix_bot.client import MatrixClient
|
||||
|
||||
|
||||
def properly_fail(function):
|
||||
"""use this decorator so that your async callback never crash, log the error and return a message to the room"""
|
||||
def properly_fail(error_message="failed to answer", style="m.notice"):
|
||||
def custom_properly_fail(function):
|
||||
"""use this decorator so that your async callback never crash, log the error and return a message to the room"""
|
||||
|
||||
@wraps(function)
|
||||
def decorated(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
|
||||
function_instance = function(room, message, matrix_client)
|
||||
@wraps(function)
|
||||
def decorated(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
|
||||
function_instance = function(room, message, matrix_client)
|
||||
|
||||
async def inner():
|
||||
try:
|
||||
return await function_instance
|
||||
except Exception as unexpected_exception: # noqa
|
||||
await matrix_client.send_text_message(room.room_id, "Oups, j'ai buggué 😿", "m.notice")
|
||||
logger.warning(f"command failed with exception : {unexpected_exception}")
|
||||
exit()
|
||||
finally:
|
||||
await matrix_client.room_typing(room.room_id, typing_state=False)
|
||||
async def inner():
|
||||
try:
|
||||
return await function_instance
|
||||
except Exception as unexpected_exception: # noqa
|
||||
await matrix_client.send_text_message(room.room_id, error_message, style)
|
||||
logger.warning(f"command failed with exception : {unexpected_exception}")
|
||||
exit()
|
||||
finally:
|
||||
await matrix_client.room_typing(room.room_id, typing_state=False)
|
||||
|
||||
return inner()
|
||||
return inner()
|
||||
|
||||
return decorated
|
||||
return decorated
|
||||
return custom_properly_fail
|
||||
Reference in New Issue
Block a user