diff sat/plugins/plugin_comp_ap_gateway/__init__.py @ 3880:03761f8ba8bb

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`.
author Goffi <goffi@goffi.org>
date Wed, 31 Aug 2022 17:07:01 +0200
parents c0bcbcf5b4b7
children 2e4a0f6050bd
line wrap: on
line diff
--- 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(