component AP gateway: raise exceptions on apGet + fix exceptions catching in apGetLocalObject:

new `exceptions.ExternalRequestError` is raised when request done `apGet` has a return
code >= 300.

Fix wrong exception catched in `apGetLocalObject`.
This commit is contained in:
Goffi 2022-08-31 17:07:01 +02:00
commit c437424053
2 changed files with 20 additions and 8 deletions

View file

@ -77,6 +77,10 @@ class DataError(Exception):
pass
class ExternalRequestError(Exception):
"""Request to third party server failed"""
class ConflictError(Exception):
pass

View file

@ -325,14 +325,22 @@ class APGateway:
@raise error.StanzaError: "service-unavailable" is sent when something went wrong
with AP server
"""
resp = await treq.get(
url,
headers = {
"Accept": [MEDIA_TYPE_AP],
"Content-Type": [MEDIA_TYPE_AP],
}
)
if resp.code >= 300:
text = await resp.text()
if resp.code == 404:
raise exceptions.NotFound()
else:
msg = f"HTTP error {resp.code}: {text}"
raise exceptions.ExternalRequestError(msg)
try:
return await treq.json_content(await treq.get(
url,
headers = {
"Accept": [MEDIA_TYPE_AP],
"Content-Type": [MEDIA_TYPE_AP],
}
))
return await treq.json_content(resp)
except Exception as e:
raise error.StanzaError(
"service-unavailable",
@ -422,7 +430,7 @@ class APGateway:
)
try:
found_item = found_items[0]
except KeyError:
except IndexError:
raise exceptions.NotFound("requested items can't be found")
mb_data = await self._m.item2mbdata(