Permalink
Browse files

Example 19.5 One way to deal with login errors.

Through a lot of trial and error, this method still connects to gmail (reconnect_max_attempts was required for that) and deals with bad servers, bad usernames, and invalid credentials. It just prints an error to the console, but it would be trivial to update it to show an error message in a popup.

However, running this code locks up the interface in an unappealing way while it waits for a connection and requests the buddy list. While this happens, it is not possible to interact with the buttons or text boxes. So let's try a different solution.
  • Loading branch information...
1 parent e7331de commit e71364815a49553af27496998299475015e9b5a3 Dusty Phillips committed Jun 29, 2013
Showing with 21 additions and 7 deletions.
  1. +21 −7 orkiv/__main__.py
View
@@ -2,6 +2,8 @@
from kivy.uix.anchorlayout import AnchorLayout
from kivy.properties import ObjectProperty
from sleekxmpp import ClientXMPP
+from sleekxmpp.exceptions import XMPPError
+from sleekxmpp.jid import InvalidJID
from kivy.uix.textinput import TextInput
@@ -24,20 +26,32 @@ class AccountDetailsForm(AnchorLayout):
password_box = ObjectProperty()
def login(self):
- jabber_id = self.username_box.text + "@" + self.server_box.text
- password = self.password_box.text
+ try:
+ jabber_id = self.username_box.text + "@" + self.server_box.text
+ password = self.password_box.text
- app = Orkiv.get_running_app()
- app.connect_to_jabber(jabber_id, password)
- print(app.xmpp.client_roster.keys())
- app.xmpp.disconnect()
+ app = Orkiv.get_running_app()
+ app.connect_to_jabber(jabber_id, password)
+ print(app.xmpp.client_roster.keys())
+ except (XMPPError, InvalidJID):
+ print("Sorry, couldn't connect, check your credentials")
+ finally:
+ if app.xmpp:
+ app.xmpp.disconnect()
class Orkiv(App):
+ def __init__(self):
+ super(Orkiv, self).__init__()
+ self.xmpp = None
+
def connect_to_jabber(self, jabber_id, password):
self.xmpp = ClientXMPP(jabber_id, password)
- self.xmpp.connect()
+ self.xmpp.reconnect_max_attempts = 1
+ connected = self.xmpp.connect()
+ if not connected:
+ raise XMPPError()
self.xmpp.process()
self.xmpp.send_presence()
self.xmpp.get_roster()

0 comments on commit e713648

Please sign in to comment.