xmx
xmx

Reputation: 137

Error handling in python with ldap

I have this bit of code below, it is part of a python script iv been working on(piecing it together blocks at a time as learning curve). This bit binds to an ldap directory to query, so the rest of the script can to the queries. When successful, it will print the below message in the block. When not successful it will throw an error- or at least i want to control the error.

If im not domain bound/vpn it will throw this message:

{'desc': "Can't contact LDAP server"}

if incorrect credentials :

Invalid credentials

nowhere in my script is it defined for the error message, how can i find where its fetching what to print that messsage- and possibly create or customize it? (for what its worth i am using PyCharm)

try:
    l = ldap.open(server)
    l.simple_bind_s(user, pwd)
    #if connection is successful print:
    print "successfully bound to %s.\n" %server
    l.protocol_version = ldap.VERSION3
except ldap.LDAPError, e:
    print e

thanks

Upvotes: 2

Views: 5808

Answers (1)

jprockbelly
jprockbelly

Reputation: 1607

you can do something like this to provide a specific message for a specific exception.

try:
    foo = 'hi'
    bar = 'hello'
    #do stuff
except ValueError:
    raise ValueError("invalid credientials: {},{}".format(foo, bar))

so in your example it could become

except ldap.LDAPError:
    raise ldap.LDAPError("invalid credientials: {},{}".format(user, pwd))

or if you literally just want to print it

except ldap.LDAPError:
    print("invalid credientials: {},{}".format(user, pwd))

Upvotes: 1

Related Questions