changeset 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 46930301f0c1
children 2e4a0f6050bd
files sat/core/exceptions.py sat/plugins/plugin_comp_ap_gateway/__init__.py
diffstat 2 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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
 
--- 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(