diff sat/plugins/plugin_xep_0166.py @ 3527:bbf92ef05f38

plugin XEP-0166, XEP-0234: better management of `terminate`: - new `text` argument to specify human readable reason of termination - handling of `FirstError` (used when a error happens in a DeferredList) - handling of `StanzaError` - (XEP-0234): show human readable text of the reason if present
author Goffi <goffi@goffi.org>
date Wed, 05 May 2021 15:37:33 +0200
parents be6d91572633
children 8e7d5796fb23
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0166.py	Wed May 05 15:37:33 2021 +0200
+++ b/sat/plugins/plugin_xep_0166.py	Wed May 05 15:37:33 2021 +0200
@@ -155,7 +155,7 @@
     def _terminateEb(self, failure_):
         log.warning(_("Error while terminating session: {msg}").format(msg=failure_))
 
-    def terminate(self, client, reason, session):
+    def terminate(self, client, reason, session, text=None):
         """Terminate the session
 
         send the session-terminate action, and delete the session data
@@ -172,6 +172,8 @@
         else:
             for elt in reason:
                 reason_elt.addChild(elt)
+        if text is not None:
+            reason_elt.addElement("text", content=text)
         self._delSession(client, session["id"])
         d = iq_elt.send()
         d.addErrback(self._terminateEb)
@@ -203,11 +205,17 @@
         @param client: %(doc_client)s
         """
         log.warning(f"Error while processing jingle request [{client.profile}]")
+        if isinstance(failure_.value, defer.FirstError):
+            failure_ = failure_.value.subFailure.value
         if isinstance(failure_, exceptions.DataError):
-            return self.sendError(client, "bad-request", session['id'], request)
+            return self.sendError(client, "bad-request", session["id"], request)
+        elif isinstance(failure_, error.StanzaError):
+            return self.terminate(client, self.REASON_FAILED_APPLICATION, session,
+                                  text=str(failure_))
         else:
             log.error(f"Unmanaged jingle exception: {failure_}")
-            return self.terminate(client, self.REASON_FAILED_APPLICATION, session)
+            return self.terminate(client, self.REASON_FAILED_APPLICATION, session,
+                                  text=str(failure_))
 
     ## methods used by other plugins ##