Reputation: 2791
I'm using the new style middleware in Django, and for some reason, it never gets to the 'process_response' part. If I understand correctly, it should occur after the
response = self.get_response(request)
in __call__
function. But it never gets to the code beyond that line. What could be the reason?
This is how my middleware is defined:
class GlobalThreadVarMiddleware(object):
_threadmap = {}
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
self._threadmap[_thread.get_ident()] = {}
self._threadmap[_thread.get_ident()]['data_manager'] = DataManager()
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
current_data_manager = self.get_current_data_manager()
current_data_manager.trigger_events()
del self._threadmap[_thread.get_ident()]['data_manager']
return response
@classmethod
def get_current_data_manager(cls):
return cls._threadmap[_thread.get_ident()]['data_manager']
I'm using Django 1.10. This is the MIDDLEWARE
parameter in settings.py:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'workflows.api.middleware.ApiCsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'workflows.api.middleware.ExceptionMiddleware',
'workflows.api.global_thread_variables_middleware.GlobalThreadVarMiddleware'
]
It's the last one in the list.
This is the ExceptionMiddleware code:
class ExceptionMiddleware(MiddlewareMixin):
def process_exception(self, request, exception):
guid, code, error, data, error_status = ExceptionMiddleware.log_exception(request, exception)
return JsonResponse(
{
"error_id": guid,
"code": code,
"message": error,
"data": data
},
status=error_status
)
@staticmethod
def log_exception(request, exception):
guid = str(uuid.uuid4())
error = code = data = error_status = None
if isinstance(exception, FlowException):
error = str(exception.message)
code = exception.code
data = str(exception.data)
error_status = exception.code
elif isinstance(exception, Exception):
error = str(exception.args)
code = 10001
data = {'exception_type': str(type(exception))}
error_status = status.HTTP_500_INTERNAL_SERVER_ERROR
logging.getLogger(__name__).exception('error_id: ' + guid,
{'request': request, 'error_data': data, 'error_code': code,
'error_message': error})
kwargs = {} #"exc": exception, "tb": exception.__traceback__, "value": error}
if newrelic.agent.current_transaction() is None:
app = newrelic.agent.application()
if app.active is False:
app.activate()
newrelic.agent.register_application()
kwargs["application"] = app
newrelic.agent.record_exception(**kwargs)
return guid, code, error, data, error_status
Upvotes: 0
Views: 792
Reputation: 3364
I can't comment, because of my bad reputation.
I've seen your question before you edited it and it included ImportError
that was directly related to your problem. Did you fix that ImportError
?
New middleware style in Django works as advertised. I have implemented this new style many times and just to be sure, I even tested your code with certain modification to get it working and there is nothing wrong with it in general.
Upvotes: 1