diff --git a/backend/benefit/companies/tests/test_api.py b/backend/benefit/companies/tests/test_api.py index 068a01cda0..5c87c4288b 100644 --- a/backend/benefit/companies/tests/test_api.py +++ b/backend/benefit/companies/tests/test_api.py @@ -79,7 +79,7 @@ def test_get_company_from_service_bus_invalid_response( "BusinessId" ] response = deepcopy(DUMMY_SERVICE_BUS_RESPONSE) - response["GetCompanyResult"]["Company"]["PostalAddress"] = {} + response["GetCompanyResult"]["Company"] = {} matcher = re.compile(re.escape(SERVICE_BUS_INFO_PATH)) requests_mock.post(matcher, json=response) diff --git a/backend/shared/shared/service_bus/service_bus_client.py b/backend/shared/shared/service_bus/service_bus_client.py index 6487b7e555..bfd5dc3e57 100644 --- a/backend/shared/shared/service_bus/service_bus_client.py +++ b/backend/shared/shared/service_bus/service_bus_client.py @@ -70,7 +70,12 @@ def _get_organisation_data_from_service_bus_data( that hasn't been covered in the code """ - address = cls._get_address(service_bus_data["PostalAddress"]["DomesticAddress"]) + try: + address = cls._get_address( + service_bus_data["PostalAddress"]["DomesticAddress"] + ) + except (KeyError, TypeError): + address = {"StreetAddress": "", "PostalCode": "", "City": ""} company_data = { "name": service_bus_data["TradeName"]["Name"], "business_id": service_bus_data["BusinessId"], @@ -152,10 +157,10 @@ def _get_company_form_code(legal_form_json: dict) -> int: raise ValueError("Cannot determine company form - invalid PrimaryCode") try: return int(legal_form_json["Type"]["SecondaryCode"]) - except (TypeError, ValueError) as e: + except (TypeError, ValueError) as exc: raise ValueError( "Cannot determine company form - invalid SecondaryCode" - ) from e + ) from exc @staticmethod def _get_finnish_description(descriptions: dict) -> Optional[dict]: