2

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

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

2 Comments

I see, so currently where is it fetching the current error message {'desc': "Can't contact LDAP server"} since i have not defined it yet?
@Xtos - yes that appears to be the defualt error message, a dict containing a descrption. See docs for LDAPError

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.