Skip to content

Commit

Permalink
fix: gère les erreurs 5XX de l'API externe ratios financiers
Browse files Browse the repository at this point in the history
des erreurs 500 et 502 sont survenues depuis hier
elles provoquaient une exception JSONDecodeError dans notre code
  • Loading branch information
emillumine committed May 29, 2024
1 parent 1a871ce commit 85d3f4c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
40 changes: 23 additions & 17 deletions impact/api/ratios_financiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import requests
import sentry_sdk

from api.exceptions import API_ERROR_SENTRY_MESSAGE
from api.exceptions import APIError
from api.exceptions import ServerError
from entreprises.models import CaracteristiquesAnnuelles

NOM_API = "ratios financiers"
RATIOS_FINANCIERS_TIMEOUT = 10


Expand All @@ -27,24 +30,27 @@ def dernier_exercice_comptable(siren):
sentry_sdk.capture_exception(e)
raise APIError()

try:
record = response.json()["records"][0]
fields = record["fields"]
donnees_financieres["date_cloture_exercice"] = _extrait_date_cloture_exercice(
fields
)
donnees_financieres.update(_extrait_chiffre_affaires(fields))
except IndexError:
pass

try:
record = response.json()["records"][1]
fields = record["fields"]
date_cloture_exercice = _extrait_date_cloture_exercice(fields)
if date_cloture_exercice == donnees_financieres["date_cloture_exercice"]:
if response.status_code == 200:
try:
record = response.json()["records"][0]
fields = record["fields"]
donnees_financieres[
"date_cloture_exercice"
] = _extrait_date_cloture_exercice(fields)
donnees_financieres.update(_extrait_chiffre_affaires(fields))
except IndexError:
pass
except IndexError:
pass
try:
record = response.json()["records"][1]
fields = record["fields"]
date_cloture_exercice = _extrait_date_cloture_exercice(fields)
if date_cloture_exercice == donnees_financieres["date_cloture_exercice"]:
donnees_financieres.update(_extrait_chiffre_affaires(fields))
except IndexError:
pass
else:
sentry_sdk.capture_message(API_ERROR_SENTRY_MESSAGE.format(NOM_API))
raise ServerError()
return donnees_financieres


Expand Down
11 changes: 11 additions & 0 deletions impact/api/tests/test_ratios_financiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from requests.exceptions import Timeout

from api.exceptions import APIError
from api.exceptions import ServerError
from api.ratios_financiers import dernier_exercice_comptable
from api.ratios_financiers import RATIOS_FINANCIERS_TIMEOUT
from api.tests import MockedResponse
Expand Down Expand Up @@ -123,3 +124,13 @@ def test_echec_ratio_financiers_exception_provoquee_par_l_api(mocker):
capture_exception_mock.assert_called_once()
args, _ = capture_exception_mock.call_args
assert type(args[0]) == Timeout


def test_echec_erreur_de_l_API(mocker):
mocker.patch("requests.get", return_value=MockedResponse(500))
capture_message_mock = mocker.patch("sentry_sdk.capture_message")

with pytest.raises(ServerError) as e:
dernier_exercice_comptable(SIREN)

capture_message_mock.assert_called_once_with("Erreur API ratios financiers")

0 comments on commit 85d3f4c

Please sign in to comment.