diff --git a/sat/core/exceptions.py b/sat/core/exceptions.py index 9b5ac4dc..40dbdf8c 100644 --- a/sat/core/exceptions.py +++ b/sat/core/exceptions.py @@ -77,6 +77,10 @@ class DataError(Exception): pass +class ExternalRequestError(Exception): + """Request to third party server failed""" + + class ConflictError(Exception): pass diff --git a/sat/plugins/plugin_comp_ap_gateway/__init__.py b/sat/plugins/plugin_comp_ap_gateway/__init__.py index 8f5a082d..8d5c0c8f 100644 --- a/sat/plugins/plugin_comp_ap_gateway/__init__.py +++ b/sat/plugins/plugin_comp_ap_gateway/__init__.py @@ -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(