diff sat_frontends/jp/cmd_account.py @ 3435:df775db4c49b

jp (account): better exit code handling
author Goffi <goffi@goffi.org>
date Fri, 04 Dec 2020 12:32:56 +0100
parents 559a625a236b
children be6d91572633
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_account.py	Fri Dec 04 12:32:05 2020 +0100
+++ b/sat_frontends/jp/cmd_account.py	Fri Dec 04 12:32:56 2020 +0100
@@ -20,13 +20,15 @@
 """This module permits to manage XMPP accounts using in-band registration (XEP-0077)"""
 
 from sat_frontends.jp.constants import Const as C
+from sat_frontends.bridge.bridge_frontend import BridgeException
 from sat.core.log import getLogger
-
-log = getLogger(__name__)
 from sat.core.i18n import _
 from sat_frontends.jp import base
 from sat_frontends.tools import jid
 
+
+log = getLogger(__name__)
+
 __commands__ = ["Account"]
 
 
@@ -83,11 +85,22 @@
                 self.args.host,
                 self.args.port,
             )
+
+        except BridgeException as e:
+            if e.condition == 'conflict':
+                self.disp(
+                    f"The account {self.args.jid} already exists",
+                    error=True
+                )
+                self.host.quit(C.EXIT_CONFLICT)
+            else:
+                self.disp(
+                    f"can't create account on {self.args.host or 'localhost'!r} with jid "
+                    f"{self.args.jid!r} using In-Band Registration: {e}", error=True)
+                self.host.quit(C.EXIT_BRIDGE_ERRBACK)
         except Exception as e:
-            self.disp(
-                f"can't create account on {self.args.host or 'localhost'!r} with jid "
-                f"{self.args.jid!r} using In-Band Registration: {e}", error=True)
-            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+            self.disp(f"Internal error: {e}", error=True)
+            self.host.quit(C.EXIT_INTERNAL_ERROR)
 
         self.disp(_("XMPP account created"), 1)
 
@@ -102,13 +115,23 @@
                 self.args.password,
                 "",
             )
+        except BridgeException as e:
+            if e.condition == 'conflict':
+                self.disp(
+                    f"The profile {self.args.profile} already exists",
+                    error=True
+                )
+                self.host.quit(C.EXIT_CONFLICT)
+            else:
+                self.disp(
+                    _(f"Can't create profile {self.args.profile} to associate with jid "
+                      f"{self.args.jid}: {e}"),
+                    error=True,
+                )
+                self.host.quit(C.EXIT_BRIDGE_ERRBACK)
         except Exception as e:
-            self.disp(
-                _(f"Can't create profile {self.args.profile} to associate with jid "
-                  f"{self.args.jid}: {e}"),
-                error=True,
-            )
-            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+            self.disp(f"Internal error: {e}", error=True)
+            self.host.quit(C.EXIT_INTERNAL_ERROR)
 
         self.disp(_("profile created"), 1)
         try: