utils.soap: intercept RequestException and convert to SOAPError (#76609)
gitea/passerelle/pipeline/head There was a failure building this commit Details

This commit is contained in:
Benjamin Dauvergne 2023-04-13 16:02:43 +02:00
parent 43847c831d
commit 47984a11c2
4 changed files with 21 additions and 17 deletions

View File

@ -30,7 +30,6 @@ from django.utils import dateformat
from django.utils.dateparse import parse_date
from django.utils.text import slugify
from django.utils.timezone import now
from requests import RequestException
from zeep.helpers import serialize_object
from zeep.wsse.username import UsernameToken
@ -39,7 +38,7 @@ from passerelle.base.models import BaseResource, HTTPResource
from passerelle.utils.api import endpoint
from passerelle.utils.conversion import simplify
from passerelle.utils.jsonresponse import APIError
from passerelle.utils.soap import SOAPError
from passerelle.utils.soap import SOAPFault, SOAPServiceUnreachable
from passerelle.utils.templates import render_to_string
from . import activity_schemas, family_schemas, invoice_schemas, schemas, utils
@ -2644,7 +2643,7 @@ class ToulouseMaelis(BaseResource, HTTPResource):
# do not use cache, except on timeout
try:
response = self.get_activity_catalog_raw(reference_year)
except RequestException:
except SOAPServiceUnreachable:
pass
else:
data = [
@ -3851,7 +3850,7 @@ class ToulouseMaelis(BaseResource, HTTPResource):
dateStart='1970-01-01',
dateEnd=now().strftime(utils.json_date_format),
)
except RequestException:
except SOAPServiceUnreachable:
pass
else:
last_update = now()
@ -4022,7 +4021,7 @@ class ToulouseMaelis(BaseResource, HTTPResource):
'numInvoice': real_invoice_id,
},
)
except SOAPError:
except SOAPFault:
raise Http404('Fichier PDF non trouvé')
response = HttpResponse(content_type='application/pdf')

View File

@ -36,10 +36,10 @@ class SOAPError(APIError):
class SOAPServiceUnreachable(SOAPError):
def __init__(self, client, exception):
super().__init__(
f'SOAP service at {client.wsdl.location} is unreachable. Please contact its administrator',
f'SOAP service at {client.wsdl.location} is unreachable. Please contact its administrator: {exception}',
data={
'wsdl': client.wsdl.location,
'status_code': exception.status_code,
'status_code': getattr(exception, 'status_code', None),
'error': str(exception),
},
)
@ -72,6 +72,8 @@ class OperationProxyWrapper(OperationProxy):
client = self._proxy._client
try:
return super().__call__(*args, **kwargs)
except RequestException as request_exception:
raise SOAPServiceUnreachable(client, request_exception)
except TransportError as transport_error:
raise SOAPServiceUnreachable(client, transport_error)
except Fault as fault:

View File

@ -2651,9 +2651,9 @@ def test_update_rl1_connection_error(family_service, con, app):
}
Link.objects.create(resource=con, family_id='1312', name_id='local')
resp = app.post_json(url + '?NameID=local', params=params, status=500)
resp = app.post_json(url + '?NameID=local', params=params, status=200)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'No address associated with hostname'
assert 'No address associated with hostname' in resp.json['err_desc']
def test_update_rl1_wrong_referential_key_error(con, app):
@ -2741,9 +2741,9 @@ def test_create_rl2_connection_error(family_service, con, app):
}
Link.objects.create(resource=con, family_id='1312', name_id='local')
resp = app.post_json(url + '?NameID=local', params=params, status=500)
resp = app.post_json(url + '?NameID=local', params=params, status=200)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'No address associated with hostname'
assert 'No address associated with hostname' in resp.json['err_desc']
def test_create_rl2_already_exists_error(family_service, con, app):
@ -2846,9 +2846,9 @@ def test_update_rl2_connection_error(family_service, con, app):
}
Link.objects.create(resource=con, family_id='1312', name_id='local')
resp = app.post_json(url + '?NameID=local', params=params, status=500)
resp = app.post_json(url + '?NameID=local', params=params, status=200)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'No address associated with hostname'
assert 'No address associated with hostname' in resp.json['err_desc']
def test_update_rl2_not_exists_error(family_service, con, app):
@ -2969,9 +2969,9 @@ def test_create_child_connection_error(family_service, con, app):
}
Link.objects.create(resource=con, family_id='1312', name_id='local')
resp = app.post_json(url + '?NameID=local', params=params, status=500)
resp = app.post_json(url + '?NameID=local', params=params, status=200)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'No address associated with hostname'
assert 'No address associated with hostname' in resp.json['err_desc']
def test_create_child_maelis_error(family_service, con, app):
@ -3067,9 +3067,9 @@ def test_update_child_connection_error(family_service, con, app):
}
Link.objects.create(resource=con, family_id='1312', name_id='local')
resp = app.post_json(url + '?NameID=local&child_id=613880', params=params, status=500)
resp = app.post_json(url + '?NameID=local&child_id=613880', params=params, status=200)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'No address associated with hostname'
assert 'No address associated with hostname' in resp.json['err_desc']
def test_update_child_not_exists_error(family_service, con, app):

View File

@ -155,6 +155,9 @@ def test_api_error(mocked_send, caplog):
operation_proxy_call.side_effect = XMLParseError('Unexpected element')
with pytest.raises(APIError, match=r'Unexpected element'):
client.service.GetLastTradePrice(tickerSymbol='banana')
mocked_send.side_effect = requests.ConnectTimeout('too long!')
with pytest.raises(APIError, match=r'SOAP service at.*is unreachable.*too long!'):
client.service.GetLastTradePrice(tickerSymbol='banana')
@responses.activate