Skip to content

Commit

Permalink
[#1956] Return None from SSD client on RequestsException
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-sigma committed Jan 9, 2024
1 parent ca8f7ab commit 1d77ccf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/open_inwoner/ssd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def templated_request(self, **kwargs) -> Response:
headers=headers,
**auth_kwargs,
)
except requests.exceptions.RequestException:
logger.exception("Requests exception")
raise
except requests.exceptions.RequestException as e:
logger.exception("Requests exception: %s", e)
return

return response

Expand Down Expand Up @@ -142,7 +142,7 @@ def get_reports(

response = self.templated_request(bsn=bsn, dienstjaar=report_date)

if response.status_code != 200:
if not response or response.status_code != 200:
return None

jaaropgaven = get_jaaropgaven(response)
Expand Down Expand Up @@ -198,7 +198,7 @@ def get_reports(

response = self.templated_request(bsn=bsn, period=report_date)

if response.status_code != 200:
if not response or response.status_code != 200:
return None

uitkeringen = get_uitkeringen(response)
Expand Down
13 changes: 13 additions & 0 deletions src/open_inwoner/ssd/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import requests_mock
from lxml import etree
from requests.exceptions import ConnectionError

from ..client import JaaropgaveClient, UitkeringClient
from .factories import ConcreteSSDClient, SSDConfigFactory
Expand Down Expand Up @@ -125,6 +126,18 @@ def test_tsl_client_cert_and_private_key(self, mock_request_body, mock_request):
),
)

@patch("open_inwoner.ssd.client.requests.post", side_effect=ConnectionError)
def test_requests_exception(self, mock_request_body, mock_request_post):
ssd_client = ConcreteSSDClient()
ssd_client.config = SSDConfigFactory.build(
service__url="https://example.com/soap-service",
)

context = {"bsn": "dummy", "period": "dummy"}
response = ssd_client.templated_request(**context)

self.assertIsNone(response)


class UitkeringClientTest(TestCase):
@requests_mock.Mocker()
Expand Down
4 changes: 2 additions & 2 deletions src/open_inwoner/ssd/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import requests
from lxml import etree # nosec
from lxml.etree import LxmlError # nosec
from lxml.etree import LxmlError, XMLSyntaxError # nosec
from xsdata.exceptions import ParserError
from xsdata.formats.dataclass.context import XmlContext
from xsdata.formats.dataclass.parsers import XmlParser
Expand Down Expand Up @@ -45,7 +45,7 @@ def _get_report_info(
try:
tree = etree.fromstring(response.content).getroottree()
node = tree.find(info_response_node)
except LxmlError:
except (LxmlError, XMLSyntaxError):
return None

parser = XmlParser(context=XmlContext(), handler=LxmlEventHandler)
Expand Down

0 comments on commit 1d77ccf

Please sign in to comment.