jsonresponse: don't log exceptions for Http404, PermissionDenied and ObjectDoesnotExist (#11058)
This commit is contained in:
parent
4360808920
commit
3c80c165af
|
@ -7,7 +7,7 @@ import functools
|
|||
import logging
|
||||
from collections import Iterable
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
|
@ -356,7 +356,8 @@ class to_json(object):
|
|||
extras = {'method': req.method}
|
||||
if req.method == 'POST':
|
||||
extras.update({'body': req.body})
|
||||
logger.exception("Error occurred while processing request", extra=extras)
|
||||
if not isinstance(e, (Http404, PermissionDenied, ObjectDoesNotExist)):
|
||||
logger.exception("Error occurred while processing request", extra=extras)
|
||||
if int(req.GET.get('raise', 0)):
|
||||
raise
|
||||
|
||||
|
@ -366,8 +367,10 @@ class to_json(object):
|
|||
if getattr(e, 'http_status', None):
|
||||
status = e.http_status
|
||||
elif isinstance(e, ObjectDoesNotExist):
|
||||
logger.warning('object not found: %r', e, extra=extras)
|
||||
status = 404
|
||||
elif isinstance(e, PermissionDenied):
|
||||
logger.warning('Permission denied', extra=extras)
|
||||
status = 403
|
||||
else:
|
||||
status = self.error_code
|
||||
|
|
|
@ -3,6 +3,8 @@ import pytest
|
|||
import json
|
||||
|
||||
from django.test.client import RequestFactory
|
||||
from django.http import Http404
|
||||
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
|
||||
|
||||
|
||||
from passerelle.utils import to_json
|
||||
|
@ -15,6 +17,19 @@ class WrappedException(Exception):
|
|||
def wrapped_exception(req, *args, **kwargs):
|
||||
raise WrappedException
|
||||
|
||||
@to_json('api')
|
||||
def http404_exception(req, *args, **kwargs):
|
||||
raise Http404
|
||||
|
||||
@to_json('api')
|
||||
def doesnotexist_exception(req, *args, **kwargs):
|
||||
raise ObjectDoesNotExist
|
||||
|
||||
|
||||
@to_json('api')
|
||||
def permissiondenied_exception(req, *args, **kwargs):
|
||||
raise PermissionDenied
|
||||
|
||||
|
||||
def test_jsonresponselog_get(caplog):
|
||||
request = RequestFactory()
|
||||
|
@ -30,3 +45,25 @@ def test_jsonresponselog_get(caplog):
|
|||
if record.method == 'POST':
|
||||
assert hasattr(record, 'body')
|
||||
assert "Error occurred while processing request" in record.message
|
||||
|
||||
def test_jsonresponselog_http404(caplog):
|
||||
request = RequestFactory()
|
||||
http404_exception(request.get('/'))
|
||||
assert caplog.records() == []
|
||||
|
||||
def test_jsonresponselog_doesnotexist(caplog):
|
||||
request = RequestFactory()
|
||||
doesnotexist_exception(request.get('/'))
|
||||
for record in caplog.records():
|
||||
assert record.name == 'passerelle.jsonresponse'
|
||||
assert record.levelno == logging.WARNING
|
||||
assert 'object not found' in record.message
|
||||
|
||||
|
||||
def test_jsonresponselog_permissiondenied(caplog):
|
||||
request = RequestFactory()
|
||||
permissiondenied_exception(request.get('/'))
|
||||
for record in caplog.records():
|
||||
assert record.name == 'passerelle.jsonresponse'
|
||||
assert record.levelno == logging.WARNING
|
||||
assert 'Permission denied' in record.message
|
||||
|
|
Loading…
Reference in New Issue