From 6680797a721c1189ba25b43fb8f0e3fe7b69cd3a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 13 Apr 2018 09:37:03 +0200 Subject: [PATCH] #2: Do not crash when a not existing product message tries to add something to the cart --- worker.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/worker.py b/worker.py index b2c0487..615b446 100644 --- a/worker.py +++ b/worker.py @@ -312,8 +312,11 @@ class ChatWorker(threading.Thread): return # If a Add to Cart button has been pressed... elif callback.data == "cart_add": - # Get the selected product - product = cart[callback.message.message_id][0] + # Get the selected product, ensuring it exists + p = cart.get(callback.message.message_id) + if p is None: + continue + product = p[0] # Add 1 copy to the cart cart[callback.message.message_id][1] += 1 # Create the product inline keyboard @@ -354,8 +357,11 @@ class ChatWorker(threading.Thread): reply_markup=final_inline_keyboard) # If the Remove from cart button has been pressed... elif callback.data == "cart_remove": - # Get the selected product - product = cart[callback.message.message_id][0] + # Get the selected product, ensuring it exists + p = cart.get(callback.message.message_id) + if p is None: + continue + product = p[0] # Remove 1 copy from the cart if cart[callback.message.message_id][1] > 0: cart[callback.message.message_id][1] -= 1