api_particulier: do not log 404 as errors (#38721)
This commit is contained in:
parent
47805fc808
commit
ec83c19aa4
|
@ -63,6 +63,8 @@ class APIParticulier(BaseResource):
|
|||
blank=True,
|
||||
verbose_name=_('API key'))
|
||||
|
||||
log_requests_errors = False
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return self.PLATFORMS[self.platform]['url']
|
||||
|
@ -83,6 +85,7 @@ class APIParticulier(BaseResource):
|
|||
raise APIError(
|
||||
u'API-particulier platform "%s" connection error: %s' %
|
||||
(self.platform, response.status_code),
|
||||
log_error=True,
|
||||
data={
|
||||
'platform': self.platform,
|
||||
'error': six.text_type(e),
|
||||
|
@ -94,6 +97,7 @@ class APIParticulier(BaseResource):
|
|||
raise APIError(
|
||||
u'API-particulier platform "%s" returned non-JSON content with status %s: %s' %
|
||||
(self.platform, response.status_code, content),
|
||||
log_error=True,
|
||||
data={
|
||||
'status_code': response.status_code,
|
||||
'exception': six.text_type(e),
|
||||
|
@ -101,14 +105,13 @@ class APIParticulier(BaseResource):
|
|||
'content': content,
|
||||
})
|
||||
if response.status_code != 200:
|
||||
if data.get('error') == 'not_found':
|
||||
return {
|
||||
'err': 1,
|
||||
'err_desc': data.get('message', 'not-found'),
|
||||
}
|
||||
# avoid logging 404 errors indicating no matching data was found
|
||||
if data.get('error') == 'not_found' and 'incorrects ou ne correspondent' in data['message']:
|
||||
raise APIError(data.get('message', 'not-found'))
|
||||
raise APIError(
|
||||
u'API-particulier platform "%s" returned a non 200 status %s: %s' %
|
||||
(self.platform, response.status_code, data),
|
||||
log_error=True,
|
||||
data={
|
||||
'status_code': response.status_code,
|
||||
'platform': self.platform,
|
||||
|
|
|
@ -16,12 +16,14 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
import pytest
|
||||
from httmock import urlmatch, HTTMock, response
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from passerelle.apps.api_particulier.models import APIParticulier
|
||||
from passerelle.base.models import ResourceLog
|
||||
|
||||
from utils import make_resource, endpoint_get
|
||||
|
||||
|
@ -246,3 +248,28 @@ def test_detail_page(app, resource):
|
|||
assert 'API Particulier Prod' in response.text
|
||||
assert 'family allowance' in response.text
|
||||
assert 'fiscal information' in response.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'mock,should_log', [
|
||||
(api_particulier_error_not_found, False), (api_particulier_error_500, True),
|
||||
(api_particulier_error_not_json, True)
|
||||
]
|
||||
)
|
||||
def test_api_particulier_dont_log_not_found(app, resource, mock, should_log):
|
||||
with HTTMock(mock):
|
||||
resp = endpoint_get(
|
||||
'/api-particulier/test/avis-imposition',
|
||||
app,
|
||||
resource,
|
||||
'avis-imposition',
|
||||
params={
|
||||
'numero_fiscal': 12,
|
||||
'reference_avis': 15,
|
||||
})
|
||||
logs = ResourceLog.objects.all()
|
||||
assert logs.count() == 3
|
||||
if should_log:
|
||||
assert logs.filter(levelno=logging.ERROR).count() == 1
|
||||
else:
|
||||
assert not logs.filter(levelno=logging.ERROR).exists()
|
||||
|
|
Loading…
Reference in New Issue