From 8166f16c98dc55ebd0cd74780e484595f5c1f5cc Mon Sep 17 00:00:00 2001 From: Luca Vari Date: Fri, 28 Jun 2024 14:38:24 +1000 Subject: [PATCH] Add guard condition to prevent intermittent KeyError There are some circumstances where the context dictionary does not contain a "request" item. In those cases we get a KeyError, for example: KeyError /clerk/api/email/{pk}/{email_id}/attachment/{attachment_id}/sharepoint/ Level: Error Unhandled 'request' Those circumstances are unclear however. It seems to be related to the issue that was addressed in commit da954958c6713837d23e4098e952d769ab24db8a but I cannot reproduce it. This change seems like a reasonable precaution until we have more information. --- app/web/templatetags/cookie.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/web/templatetags/cookie.py b/app/web/templatetags/cookie.py index 4a43734c..ab45f4b4 100644 --- a/app/web/templatetags/cookie.py +++ b/app/web/templatetags/cookie.py @@ -5,6 +5,7 @@ @register.simple_tag(takes_context=True) def get_cookie(context, cookie_name): - request = context["request"] - result = request.COOKIES.get(cookie_name, '') - return result + request = context.get("request") + if request: + return request.COOKIES.get(cookie_name, "") + return ""