Repository navigation
Conversation
|
Seems reasonable. Probably also needs some docs changes too? |
|
@tomchristie sure here it is: my settings.py : REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'core.exceptions.exception_handler',
}my handler: def exception_handler(exc, drf_request=None):
"""
Replace request.POST with drf_request.DATA so Sentry
can log request properly.
@param exc: Exception
@param drf_request: Request
@return: None
"""
response = views.exception_handler(exc)
if response is None and drf_request is not None \
and drf_request.method not in ['GET', 'HEAD', 'OPTIONS']:
request = get_request()
request.POST = drf_request.DATA
return response |
|
This looks precisely what I was looking for! Could you also supply the definition of |
|
@kot-behemoth take a look at original DRF exception handler. |
|
Not happy with pushing the request object into the exception handling API. Just doesn't feel like the right thing to do. Yes it's awkward that request.DATA isn't available to sentry by default, yes it would be better if it was, but redesigning the exception handling API in order to support that use case doesn't seem to hit the design sweet spot. Ideally we'd have |
|
@tomchristie well this makes sentry and other logging apps useles. if there is no way to get a request data then the is no way for easy issues investigation :/ what is so wrong about configurable exception handler that have access to all possible information? |
|
I'd be more interested for someone to investigate if this can instead be handled in, say, middleware in some sensible way. Yes, having |
|
well I've tried to do this functionality in a middleware, but I have a good way to do it without copying request object, which is bad idea |
|
@pySilver Want to give an example of the bad way around of doing it - might be a starting point for us figuring out a decent way to support it with middleware without having to repurpose the exception handling API. |
|
Reconsidering. See #2236. |
|
After #2236, you could do something ala |
|
@vesterbaek workaround is far better than previous one. You'll get this exception: Traceback:
File "django/core/handlers/base.py" in get_response
223. response = middleware_method(request, response)
File "debug_toolbar/middleware.py" in process_response
94. new_response = panel.process_response(request, response)
File "debug_toolbar/panels/request.py" in process_response
31. 'post': [(k, request.POST.getlist(k)) for k in sorted(request.POST)],
File "debug_toolbar/panels/request.py" in <listcomp>
31. 'post': [(k, request.POST.getlist(k)) for k in sorted(request.POST)],
Exception Type: AttributeError at /my-url
Exception Value: 'dict' object has no attribute 'getlist'I used this workaround's workaround: def exception_handler(exc, context):
from rest_framework import views
from django.http.request import QueryDict
context['request']._request.POST = QueryDict(str(context['request'].data))
return views.exception_handler(exc, context) |
|
@socketubs, @vesterbaek original request.post won't be available the moment it's consumed, so you cannot substitute its later. And most importantly request.POST != request.data and it should not be. |
|
@socketubs : your approach worked, but the def exception_handler(exc, context):
"""
See: https://github.com/tomchristie/django-rest-framework/pull/1671#issuecomment-169378632
"""
from rest_framework import views
from django.http.request import QueryDict
query = QueryDict('', mutable=True)
query.update(context['request'].data)
context['request']._request.POST = query
return views.exception_handler(exc, context) |
|
@silviogutierrez it will fail if |
This PR allows loggers such as Sentry get access to a request object constructed by the DRF using custom exception handler.
Problem was fully described here: #1380