From c09aa07997401b177a747a244a64e8455c725234 Mon Sep 17 00:00:00 2001 From: Kwan-Yuet Ho Date: Sat, 8 Apr 2023 18:09:59 -0400 Subject: [PATCH] better exception handling --- main.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index 564d04f..fc9b1a6 100644 --- a/main.py +++ b/main.py @@ -456,26 +456,32 @@ def sending_index_ma(message): def handle_maplot_callback_query(call): - callbackstr = call.data + if isinstance(call, telebot.types.CallbackQuery): + callbackstr = call.data + + if callbackstr == 'button_maplot_sp500': + index = '^GSPC' + plottitle = 'S&P 500 (^GSPC)' + elif callbackstr == 'button_maplot_nasdaq': + index = '^IXIC' + plottitle = 'NASDAQ (^IXIC)' + elif callbackstr == 'button_maplot_dji': + index = '^DJI' + plottitle = 'Dow Jones (^DJI)' + else: + return {} - if callbackstr == 'button_maplot_sp500': - index = '^GSPC' - plottitle = 'S&P 500 (^GSPC)' - elif callbackstr == 'button_maplot_nasdaq': - index = '^IXIC' - plottitle = 'NASDAQ (^IXIC)' - elif callbackstr == 'button_maplot_dji': - index = '^DJI' - plottitle = 'Dow Jones (^DJI)' + plot_info = plotting_index_ma(index, plottitle) + f = urllib.request.urlopen(plot_info['plot']['url']) + bot.send_photo(call.from_user.id, f) + return { + 'ploturl': plot_info['plot']['url'] + } + elif isinstance(call, telebot.types.Message): + bot.send_message(call.id, 'Internal error. Try again!') else: - return {} - - plot_info = plotting_index_ma(index, plottitle) - f = urllib.request.urlopen(plot_info['plot']['url']) - bot.send_photo(call.from_user.id, f) - return { - 'ploturl': plot_info['plot']['url'] - } + logging.error('Unknown error!') + print('Unknown error!', file=sys.stderr) def lambda_handler(event, context):