How to more easily diagnose the reason for Bad Request #1676
-
Today I spent a long time trying to figure out why a caller was getting a 400 response as the only thing in the logs was this:
from Django's I eventually realised it was due to the filter being passed e.g. This is the basics of the code in question: class MyFilterSet(FilterSet):
class Meta:
model = MyModel
fields = ["foo"]
foo = MultipleChoiceFilter(choices=MyFooType.choices)
class MyViewSet(viewsets.ReadOnlyModelViewSet):
filter_backends = [DjangoFilterBackend]
filterset_class = MyFilterSet
... Whilst typing this I've realised that the actual response body probably explains exactly what the problem is and so the problem really is that the client (in my case another cloud service) should be logging not just "I got a 400 response" but also the response body at that point. So this may just be a "PEBKAC" but I'll post it anyway in case it helps someone else. Just in case though, is there any way to get the server to log more detail as to why it is about to return a 400? If there were a generic way to log the reason for any validation error on the server that would really help - though perhaps this is a DRF question rather than django-filter specifically... (I guess one could write middleware which catches |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @sparrowt. You could subclass the 'django_filters.rest_framework.DjangoFilterBackend' and override That would be pretty light-weight (and well targeted) I'd suggest. Have fun ⛱️ |
Beta Was this translation helpful? Give feedback.
Hi @sparrowt.
You could subclass the 'django_filters.rest_framework.DjangoFilterBackend' and override
filter_queryset()
to catch therest_framework.exceptions.ValidationError
and log the details.That would be pretty light-weight (and well targeted) I'd suggest.
Have fun ⛱️