changeset 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 (2021-05-05)
parents e84ffb48acd4
children 849374e59178
files sat/plugins/plugin_xep_0166.py sat/plugins/plugin_xep_0234.py
diffstat 2 files changed, 13 insertions(+), 3 deletions(-) [+]
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 ##
 
--- a/sat/plugins/plugin_xep_0234.py	Wed May 05 15:37:33 2021 +0200
+++ b/sat/plugins/plugin_xep_0234.py	Wed May 05 15:37:33 2021 +0200
@@ -698,6 +698,8 @@
             first_child = jingle_elt.firstChildElement()
             if first_child is not None:
                 reason = first_child.name
+                if jingle_elt.text is not None:
+                    reason = f"{reason} - {jingle_elt.text}"
             else:
                 reason = C.PROGRESS_ERROR_FAILED
             self.host.bridge.progressError(