From 3cff59c57b1b5d752fe06a899b4bfa4b122d42a5 Mon Sep 17 00:00:00 2001 From: Jesse Vickery Date: Fri, 25 Aug 2023 14:16:29 +0000 Subject: [PATCH] fix(dev): catch no context for middleware; - Catch `TypeError` if there is no registered context for the request. --- ckanext/canada/plugins.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ckanext/canada/plugins.py b/ckanext/canada/plugins.py index ab8c65d01..ca78fa57d 100755 --- a/ckanext/canada/plugins.py +++ b/ckanext/canada/plugins.py @@ -12,6 +12,7 @@ from ckan.plugins.toolkit import ( c, + g, h, chained_action, ValidationError, @@ -702,11 +703,15 @@ def __init__(self, app, config): def __call__(self, environ, start_response): def _start_response(status, response_headers, exc_info=None): extra = [] - if c.user: - log_extra = c.log_extra if hasattr(c, 'log_extra') else u'' + try: + contextual_user = g.user + except TypeError: + contextual_user = None + if contextual_user: + log_extra = g.log_extra if hasattr(g, 'log_extra') else u'' extra = [( 'X-LogExtra', u'user={uid} {extra}'.format( - uid=c.user, + uid=contextual_user, extra=log_extra).encode('utf-8') ) ]