From: Nick B. <ni...@br...> - 2001-10-23 12:46:52
|
Is there any possible way to tell what exception has been raised in python-ldap without catching every single possibile type of _ldap.LDAPError? It would be really nice in example included in the python-ldap documentation if an exception was caught in the following general way: try: <connect, bind and search, as is currently shown> except: <display type of exception and its description> at the moment, there is no example code with exceptions in the documentation or distribution and i can't figure out how to catch one from the exception references provided without trying to catch every single possibility. thanks alot, nick. |
From: Donal H. <don...@ma...> - 2001-10-23 12:53:30
|
Nick, try this: ----------------------------------------------------------- try: <connect, bind and search, as is currently shown> except Exception, e: print 'Error! %s' % e ----------------------------------------------------------- Regards Donal Hunt Nick Bower wrote: > > Is there any possible way to tell what exception has been raised in > python-ldap without catching every single possibile type of > _ldap.LDAPError? > > It would be really nice in example included in the python-ldap > documentation if an exception was caught in the following general way: > > try: > <connect, bind and search, as is currently shown> > except: > <display type of exception and its description> > > at the moment, there is no example code with exceptions in the > documentation or distribution and i can't figure out how to catch one > from the exception references provided without trying to catch every > single possibility. > > thanks alot, nick. > > _______________________________________________ > Python-LDAP-dev mailing list > Pyt...@li... > https://lists.sourceforge.net/lists/listinfo/python-ldap-dev |
From: Michael <mi...@st...> - 2001-10-24 10:17:23
|
Nick Bower wrote: > > Is there any possible way to tell what exception has been raised in > python-ldap without catching every single possibile type of > _ldap.LDAPError? > > It would be really nice in example included in the python-ldap > documentation if an exception was caught in the following general way: > > try: > <connect, bind and search, as is currently shown> > except: > <display type of exception and its description> > > at the moment, there is no example code with exceptions in the > documentation or distribution and i can't figure out how to catch one > from the exception references provided without trying to catch every > single possibility. import ldap try: <connect, bind and search, as is currently shown> except ldap.LDAPError,e: print repr(e) Well, exception instances are not really implemented completely pythonic but it's a start. Ciao, Michael. |
From: Nick B. <ni...@br...> - 2001-10-24 10:23:00
|
thanks - i was having trouble knowing where this "desc" field was. so i se now that it is: except _ldap.LDAPError, e print 'Your error is ' + e[0]['desc'] thanks for the help. nick Michael Str=F6der wrote: >=20 > Nick Bower wrote: > > > > Is there any possible way to tell what exception has been raised in > > python-ldap without catching every single possibile type of > > _ldap.LDAPError? > > > > It would be really nice in example included in the python-ldap > > documentation if an exception was caught in the following general way= : > > > > try: > > <connect, bind and search, as is currently shown> > > except: > > <display type of exception and its description> > > > > at the moment, there is no example code with exceptions in the > > documentation or distribution and i can't figure out how to catch one > > from the exception references provided without trying to catch every > > single possibility. >=20 > import ldap >=20 > try: > <connect, bind and search, as is currently shown> > except ldap.LDAPError,e: > print repr(e) >=20 > Well, exception instances are not really implemented completely > pythonic but it's a start. >=20 > Ciao, Michael. --=20 Nick Bower, Intranet Developer ni...@br... Brainstorm 388 - 396 Oxford St London W1N 9EH United Kingdom Tel 020 7074 7000 Mob 0790 5405 443 Fax 020 7074 7070 http://www.brainstorm.co.uk |
From: Michael <mi...@st...> - 2001-10-24 12:41:09
|
Nick Bower wrote: > > thanks - i was having trouble knowing where this "desc" field was. so i > se now that it is: > > except _ldap.LDAPError, e > print 'Your error is ' + e[0]['desc'] > > thanks for the help. Sometimes you can't rely on the exact data structure with e.args being a dictionary of the form {'desc':'','info':''}. I'd also recommend to display the info field. E.g. recent versions of OpenLDAP 2 give more information about schema violation which is useful for the user. Maybe it's overkill how web2ldap does it but take the snippet below as food for thought. Note that web2ldap trys to also handle older versions of python-ldap floating around: LDAPError = ldap.LDAPError [..] except LDAPError,e: if __debug__: w2lapp.core.log_exception(errfile,sid,command,form,ls,env) try: if type(e.args[0])==type({}): ErrMsg = '%s: %s' % (e.args[0]['desc'],e.args[0].get('info','')) elif type(e.args[0])==type(1): ErrMsg = 'Error Code: %d<br />Error description: %s' % ( e.args[0],e.args[1] ) else: ErrMsg = str(e.args) except TypeError: ErrMsg = str(e.args) except IndexError: ErrMsg = str(e.args) w2lapp.gui.ExceptionMsg( sid,outf,form,ls,'LDAP exception',ErrMsg ) [..] Ciao, Michael. |