Improve error handling in user interaction responses
- Refactored the handling of callback answers and message responses to include try-except blocks, ensuring that exceptions are caught and logged without disrupting the user experience. - Updated message sending methods to utilize the `answer` method consistently, enhancing the reliability of user notifications in various scenarios.
This commit is contained in:
@@ -43,10 +43,9 @@ async def send_main_menu(target_event: Union[types.Message,
|
|||||||
await target_event.answer(err_msg_fallback, show_alert=True)
|
await target_event.answer(err_msg_fallback, show_alert=True)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
elif isinstance(target_event, types.Message) and hasattr(
|
elif isinstance(target_event, types.Message):
|
||||||
target_event, 'chat') and target_event.chat:
|
|
||||||
try:
|
try:
|
||||||
await target_event.chat.send_message(err_msg_fallback)
|
await target_event.answer(err_msg_fallback)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
return
|
return
|
||||||
@@ -93,23 +92,27 @@ async def send_main_menu(target_event: Union[types.Message,
|
|||||||
await target_message_obj.answer(text, reply_markup=reply_markup)
|
await target_message_obj.answer(text, reply_markup=reply_markup)
|
||||||
|
|
||||||
if isinstance(target_event, types.CallbackQuery):
|
if isinstance(target_event, types.CallbackQuery):
|
||||||
|
try:
|
||||||
await target_event.answer()
|
await target_event.answer()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
except Exception as e_send_edit:
|
except Exception as e_send_edit:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Failed to send/edit main menu (user: {user_id}, is_edit: {is_edit}): {type(e_send_edit).__name__} - {e_send_edit}."
|
f"Failed to send/edit main menu (user: {user_id}, is_edit: {is_edit}): {type(e_send_edit).__name__} - {e_send_edit}."
|
||||||
)
|
)
|
||||||
if is_edit and target_message_obj and hasattr(
|
if is_edit and target_message_obj:
|
||||||
target_message_obj, 'chat') and target_message_obj.chat:
|
|
||||||
try:
|
try:
|
||||||
await target_message_obj.chat.send_message(
|
await target_message_obj.answer(text, reply_markup=reply_markup)
|
||||||
text, reply_markup=reply_markup)
|
|
||||||
except Exception as e_send_new:
|
except Exception as e_send_new:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Also failed to send new main menu message for user {user_id}: {e_send_new}"
|
f"Also failed to send new main menu message for user {user_id}: {e_send_new}"
|
||||||
)
|
)
|
||||||
if isinstance(target_event, types.CallbackQuery):
|
if isinstance(target_event, types.CallbackQuery):
|
||||||
|
try:
|
||||||
await target_event.answer(
|
await target_event.answer(
|
||||||
_("error_occurred_try_again") if is_edit else None)
|
_("error_occurred_try_again") if is_edit else None)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@router.message(CommandStart())
|
@router.message(CommandStart())
|
||||||
|
|||||||
@@ -31,7 +31,10 @@ async def request_trial_confirmation_handler(
|
|||||||
_ = lambda key, **kwargs: i18n.gettext(current_lang, key, **kwargs) if i18n else key
|
_ = lambda key, **kwargs: i18n.gettext(current_lang, key, **kwargs) if i18n else key
|
||||||
|
|
||||||
if not i18n or not callback.message:
|
if not i18n or not callback.message:
|
||||||
|
try:
|
||||||
await callback.answer(_("error_occurred_try_again"), show_alert=True)
|
await callback.answer(_("error_occurred_try_again"), show_alert=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
return
|
return
|
||||||
|
|
||||||
show_trial_btn_in_menu_if_fail = False
|
show_trial_btn_in_menu_if_fail = False
|
||||||
@@ -46,7 +49,10 @@ async def request_trial_confirmation_handler(
|
|||||||
current_lang, i18n, settings, False
|
current_lang, i18n, settings, False
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
await callback.answer()
|
await callback.answer()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
return
|
return
|
||||||
|
|
||||||
if await subscription_service.has_had_any_subscription(session, user_id):
|
if await subscription_service.has_had_any_subscription(session, user_id):
|
||||||
@@ -56,7 +62,10 @@ async def request_trial_confirmation_handler(
|
|||||||
current_lang, i18n, settings, False
|
current_lang, i18n, settings, False
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
await callback.answer()
|
await callback.answer()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
return
|
return
|
||||||
|
|
||||||
# Directly activate trial without confirmation
|
# Directly activate trial without confirmation
|
||||||
@@ -68,7 +77,10 @@ async def request_trial_confirmation_handler(
|
|||||||
show_trial_button_after_action = False
|
show_trial_button_after_action = False
|
||||||
|
|
||||||
if activation_result and activation_result.get("activated"):
|
if activation_result and activation_result.get("activated"):
|
||||||
|
try:
|
||||||
await callback.answer(_("trial_activated_alert"), show_alert=True)
|
await callback.answer(_("trial_activated_alert"), show_alert=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
end_date_obj = activation_result.get("end_date")
|
end_date_obj = activation_result.get("end_date")
|
||||||
config_link_for_trial = activation_result.get("subscription_url") or _(
|
config_link_for_trial = activation_result.get("subscription_url") or _(
|
||||||
@@ -106,7 +118,10 @@ async def request_trial_confirmation_handler(
|
|||||||
else "trial_activation_failed"
|
else "trial_activation_failed"
|
||||||
)
|
)
|
||||||
final_message_text_in_chat = _(message_key_from_service)
|
final_message_text_in_chat = _(message_key_from_service)
|
||||||
|
try:
|
||||||
await callback.answer(final_message_text_in_chat, show_alert=True)
|
await callback.answer(final_message_text_in_chat, show_alert=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
if (
|
if (
|
||||||
settings.TRIAL_ENABLED
|
settings.TRIAL_ENABLED
|
||||||
and not await subscription_service.has_had_any_subscription(
|
and not await subscription_service.has_had_any_subscription(
|
||||||
@@ -129,12 +144,8 @@ async def request_trial_confirmation_handler(
|
|||||||
f"Could not edit trial result message: {e_edit}. Sending new one."
|
f"Could not edit trial result message: {e_edit}. Sending new one."
|
||||||
)
|
)
|
||||||
|
|
||||||
if (
|
if callback.message:
|
||||||
callback.message
|
await callback.message.answer(
|
||||||
and hasattr(callback.message, "chat")
|
|
||||||
and callback.message.chat
|
|
||||||
):
|
|
||||||
await callback.message.chat.send_message(
|
|
||||||
final_message_text_in_chat,
|
final_message_text_in_chat,
|
||||||
parse_mode="HTML",
|
parse_mode="HTML",
|
||||||
reply_markup=get_main_menu_inline_keyboard(
|
reply_markup=get_main_menu_inline_keyboard(
|
||||||
@@ -160,20 +171,29 @@ async def confirm_activate_trial_handler(
|
|||||||
_ = lambda key, **kwargs: i18n.gettext(current_lang, key, **kwargs) if i18n else key
|
_ = lambda key, **kwargs: i18n.gettext(current_lang, key, **kwargs) if i18n else key
|
||||||
|
|
||||||
if not i18n or not callback.message:
|
if not i18n or not callback.message:
|
||||||
|
try:
|
||||||
await callback.answer(_("error_occurred_try_again"), show_alert=True)
|
await callback.answer(_("error_occurred_try_again"), show_alert=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
return
|
return
|
||||||
|
|
||||||
if not settings.TRIAL_ENABLED:
|
if not settings.TRIAL_ENABLED:
|
||||||
|
try:
|
||||||
await callback.answer(_("trial_feature_disabled"), show_alert=True)
|
await callback.answer(_("trial_feature_disabled"), show_alert=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
await send_main_menu(
|
await send_main_menu(
|
||||||
callback, settings, i18n_data, subscription_service, session, is_edit=True
|
callback, settings, i18n_data, subscription_service, session, is_edit=True
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
if await subscription_service.has_had_any_subscription(session, user_id):
|
if await subscription_service.has_had_any_subscription(session, user_id):
|
||||||
|
try:
|
||||||
await callback.answer(
|
await callback.answer(
|
||||||
_("trial_already_had_subscription_or_trial"), show_alert=True
|
_("trial_already_had_subscription_or_trial"), show_alert=True
|
||||||
)
|
)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
await send_main_menu(
|
await send_main_menu(
|
||||||
callback, settings, i18n_data, subscription_service, session, is_edit=True
|
callback, settings, i18n_data, subscription_service, session, is_edit=True
|
||||||
)
|
)
|
||||||
@@ -187,7 +207,10 @@ async def confirm_activate_trial_handler(
|
|||||||
show_trial_button_after_action = False
|
show_trial_button_after_action = False
|
||||||
|
|
||||||
if activation_result and activation_result.get("activated"):
|
if activation_result and activation_result.get("activated"):
|
||||||
|
try:
|
||||||
await callback.answer(_("trial_activated_alert"), show_alert=True)
|
await callback.answer(_("trial_activated_alert"), show_alert=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
end_date_obj = activation_result.get("end_date")
|
end_date_obj = activation_result.get("end_date")
|
||||||
config_link_for_trial = activation_result.get("subscription_url") or _(
|
config_link_for_trial = activation_result.get("subscription_url") or _(
|
||||||
@@ -221,7 +244,10 @@ async def confirm_activate_trial_handler(
|
|||||||
else "trial_activation_failed"
|
else "trial_activation_failed"
|
||||||
)
|
)
|
||||||
final_message_text_in_chat = _(message_key_from_service)
|
final_message_text_in_chat = _(message_key_from_service)
|
||||||
|
try:
|
||||||
await callback.answer(final_message_text_in_chat, show_alert=True)
|
await callback.answer(final_message_text_in_chat, show_alert=True)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
if (
|
if (
|
||||||
settings.TRIAL_ENABLED
|
settings.TRIAL_ENABLED
|
||||||
and not await subscription_service.has_had_any_subscription(
|
and not await subscription_service.has_had_any_subscription(
|
||||||
@@ -244,12 +270,8 @@ async def confirm_activate_trial_handler(
|
|||||||
f"Could not edit trial result message: {e_edit}. Sending new one."
|
f"Could not edit trial result message: {e_edit}. Sending new one."
|
||||||
)
|
)
|
||||||
|
|
||||||
if (
|
if callback.message:
|
||||||
callback.message
|
await callback.message.answer(
|
||||||
and hasattr(callback.message, "chat")
|
|
||||||
and callback.message.chat
|
|
||||||
):
|
|
||||||
await callback.message.chat.send_message(
|
|
||||||
final_message_text_in_chat,
|
final_message_text_in_chat,
|
||||||
parse_mode="HTML",
|
parse_mode="HTML",
|
||||||
reply_markup=get_main_menu_inline_keyboard(
|
reply_markup=get_main_menu_inline_keyboard(
|
||||||
|
|||||||
Reference in New Issue
Block a user