# HG changeset patch # User Goffi # Date 1661958421 -7200 # Node ID 03761f8ba8bb0e53b8ec3b3ebdee5df4ae7b1f74 # Parent 46930301f0c15c3f6bb54f18dc046408caaa950d 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`. diff -r 46930301f0c1 -r 03761f8ba8bb sat/core/exceptions.py --- a/sat/core/exceptions.py Wed Aug 31 13:18:56 2022 +0200 +++ b/sat/core/exceptions.py Wed Aug 31 17:07:01 2022 +0200 @@ -77,6 +77,10 @@ pass +class ExternalRequestError(Exception): + """Request to third party server failed""" + + class ConflictError(Exception): pass diff -r 46930301f0c1 -r 03761f8ba8bb sat/plugins/plugin_comp_ap_gateway/__init__.py --- a/sat/plugins/plugin_comp_ap_gateway/__init__.py Wed Aug 31 13:18:56 2022 +0200 +++ b/sat/plugins/plugin_comp_ap_gateway/__init__.py Wed Aug 31 17:07:01 2022 +0200 @@ -325,14 +325,22 @@ @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 @@ ) 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(