Better error handling in the gui

This commit is contained in:
Michael Spang 2007-12-13 23:53:15 -05:00
parent 9110a41969
commit 36413de090
5 changed files with 53 additions and 23 deletions

View File

@ -111,6 +111,17 @@ def make_modlist(old, new):
return mlist return mlist
def format_ldaperror(ex):
desc = ex[0].get('desc', '')
info = ex[0].get('info', '')
if desc and info:
return "%s: %s" % (desc, info)
elif desc:
return desc
else:
return str(ex)
class Sasl: class Sasl:
def __init__(self, mech, realm): def __init__(self, mech, realm):

View File

@ -43,11 +43,13 @@ def configure():
### Exceptions ### ### Exceptions ###
LDAPException = ldap.LDAPError
ConfigurationException = conf.ConfigurationException
class MemberException(Exception): class MemberException(Exception):
"""Base exception class for member-related errors.""" """Base exception class for member-related errors."""
def __init__(self, ex):
Exception.__init__(self)
self.ex = ex
def __str__(self):
return str(self.ex)
class InvalidTerm(MemberException): class InvalidTerm(MemberException):
"""Exception class for malformed terms.""" """Exception class for malformed terms."""
@ -134,10 +136,13 @@ def create_member(username, password, name, program):
if not password or len(password) < cfg['min_password_length']: if not password or len(password) < cfg['min_password_length']:
raise InvalidArgument("password", "<hidden>", "too short (minimum %d characters)" % cfg['min_password_length']) raise InvalidArgument("password", "<hidden>", "too short (minimum %d characters)" % cfg['min_password_length'])
args = [ "/usr/bin/addmember", "--stdin", username, name, program ] try:
addmember = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) args = [ "/usr/bin/addmember", "--stdin", username, name, program ]
out, err = addmember.communicate(password) addmember = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
status = addmember.wait() out, err = addmember.communicate(password)
status = addmember.wait()
except OSError, e:
raise MemberException(e)
if status: if status:
raise ChildFailed("addmember", status, out+err) raise ChildFailed("addmember", status, out+err)
@ -324,10 +329,13 @@ def create_club(username, name):
if not username or not re.match(cfg['username_regex'], username): if not username or not re.match(cfg['username_regex'], username):
raise InvalidArgument("username", username, "expected format %s" % repr(cfg['username_regex'])) raise InvalidArgument("username", username, "expected format %s" % repr(cfg['username_regex']))
args = [ "/usr/bin/addclub", username, name ] try:
addclub = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) args = [ "/usr/bin/addclub", username, name ]
out, err = addclub.communicate() addclub = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
status = addclub.wait() out, err = addclub.communicate()
status = addclub.wait()
except OSError, e:
raise MemberException(e)
if status: if status:
raise ChildFailed("addclub", status, out+err) raise ChildFailed("addclub", status, out+err)

View File

@ -1,5 +1,5 @@
import random, ldap, urwid.curses_display import random, ldap, urwid.curses_display
from ceo import members from ceo import members, ldapi
from ceo.urwid.widgets import * from ceo.urwid.widgets import *
from ceo.urwid.window import * from ceo.urwid.window import *
from ceo.urwid import newmember, renew, info, search, positions, groups from ceo.urwid import newmember, renew, info, search, positions, groups
@ -146,8 +146,12 @@ def start():
try: try:
ui.run_wrapper( run ) ui.run_wrapper( run )
except ldap.LOCAL_ERROR, e: except ldap.LOCAL_ERROR, e:
print e[0]['info'] print ldapi.format_ldaperror(e)
print "Hint: You may need to run 'kinit'" print "Hint: You may need to run 'kinit'"
except ldap.INSUFFICIENT_ACCESS, e:
print ldapi.format_ldaperror(e)
print "You probably aren't permitted to do whatever you just tried."
print "Admittedly, ceo probably shouldn't have crashed either."
if __name__ == '__main__': if __name__ == '__main__':
start() start()

View File

@ -1,4 +1,4 @@
import urwid import ldap, urwid
from ceo import members, terms from ceo import members, terms
from ceo.urwid.widgets import * from ceo.urwid.widgets import *
from ceo.urwid.window import * from ceo.urwid.window import *
@ -159,15 +159,16 @@ class EndPage(WizardPanel):
raise Exception("Internal Error") raise Exception("Internal Error")
except members.InvalidArgument, e: except members.InvalidArgument, e:
problem = str(e) problem = str(e)
except members.LDAPException, e: except ldap.LDAPError, e:
problem = str(e) problem = str(e)
except members.ChildFailed, e: except members.MemberException, e:
problem = str(e) problem = str(e)
if problem: if problem:
self.headtext.set_text("Failures Occured Adding User") self.headtext.set_text("Failures Occured Adding User")
self.midtext.set_text("The error was:\n%s\nThe account may be partially added " self.midtext.set_text("The error was:\n\n%s\n\nThe account may be partially added "
"and you may or may not be able to log in. Please contact systems committee." % problem) "and you may or may not be able to log in. Or perhaps you are not office staff. "
"If this was not expected please contact systems committee." % problem)
return return
else: else:
self.headtext.set_text("User Added") self.headtext.set_text("User Added")

View File

@ -1,5 +1,5 @@
import urwid import urwid, ldap
from ceo import members, terms from ceo import members, terms, ldapi
from ceo.urwid.widgets import * from ceo.urwid.widgets import *
from ceo.urwid.window import * from ceo.urwid.window import *
@ -102,14 +102,20 @@ class EndPage(WizardPanel):
def focusable(self): def focusable(self):
return False return False
def activate(self): def activate(self):
problem = None
try: try:
members.register( self.state['userid'], self.state['terms'] ) members.register( self.state['userid'], self.state['terms'] )
self.headtext.set_text("Registration Succeeded") self.headtext.set_text("Registration Succeeded")
self.midtext.set_text("The member has been registered for the following " self.midtext.set_text("The member has been registered for the following "
"terms: " + ", ".join(self.state['terms']) + ".") "terms: " + ", ".join(self.state['terms']) + ".")
except Exception, e: except ldap.LDAPError, e:
problem = ldapi.format_ldaperror(e)
except members.MemberException, e:
problem = str(e)
if problem:
self.headtext.set_text("Failed to Register") self.headtext.set_text("Failed to Register")
self.midtext.set_text("You may refund any fees paid or retry." self.midtext.set_text("You may refund any fees paid or retry. "
"The error was: '%s'" % e) "The error was:\n\n%s" % problem)
def check(self): def check(self):
pop_window() pop_window()