0

I have some code that proxies LDAP messages. The code implements handlers of ApacheDS by calling LDAP servers using JNDI. When an error is returned from the LDAP server, JNDI reports it as an javax.naming.NamingException (or subclass) like:

  AuthenticationException: [LDAP: error code 49 - Invalid Credentials]

On the other hand we need to return an answer to the client through the ApacheDS sink using an error code enum:

  ResultCodeEnum resultCodeEnum = ResultCodeEnum.getResultCode(errorCode);
  ldapResult.setResultCode(resultCodeEnum);

How can I extract the "error code" number from the JNDI javax.naming.NamingException? (of course I can always parse the 'error code' from NamingException.explanation, if a number is found in explanation, but I'm looking for a library solution)

Hagai Cibulski
  • 4,421
  • 3
  • 20
  • 23
  • I don't think there is anything standard. Please refer http://stackoverflow.com/questions/3421643/is-there-a-neater-way-to-analyse-the-authenticationexception-for-the-error-code – Hirak Apr 28 '14 at 15:59
  • Check http://docs.oracle.com/javase/tutorial/jndi/ldap/exceptions.html – Jay Apr 28 '14 at 16:02
  • In case of SqlException it has e.getErrorCode() method to retrieve the error code. What is the Exception class you have ? Does it not have some thing similar to that ? – Jay Apr 28 '14 at 16:05
  • I have a javax.naming.NamingException (or subclass) – Hagai Cibulski Apr 28 '14 at 16:08
  • (edited the title and content to say NamingException) – Hagai Cibulski Apr 28 '14 at 16:11

1 Answers1

1

You need get exception message from exception using e.getMessage() this will give you exception string as AuthenticationException: [LDAP: error code 49 - Invalid Credentials]

try {
      // you stuff here .....
} catch (NamingException e) {
  int errCode= getErrorCode(e.getMessage());
}

using Java regex to get/extract error code form exception string

 private int getErrorCode(final String exceptionMsg)
    {
        String pattern="-?\\d+";
        Pattern p=Pattern.compile(pattern);
        Matcher  m=p.matcher(exceptionMsg);
        if (m.find()) {
            return Integer.valueOf(m.group(0));
        }
        return -1;
    }
Asif Bhutto
  • 3,916
  • 1
  • 24
  • 21
  • Of course I can always parse the 'error code' from NamingException.explanation, if a number is found in explanation, but I'm looking for a library solution – Hagai Cibulski Apr 30 '14 at 07:38
  • You can make custom exception class(LDAPException extend NamingException) int you custom exception class you neeed message, errorcode code properties and getter/setter, throw LDAPException to your client code and catch it, then get it e.getErrorCode(). INside Error code you extract error code. this once. there is not need of libaray – Asif Bhutto Apr 30 '14 at 07:52
  • My code does not throw the exceptions, it calls LDAP servers using JNDI. When an error is returned from the LDAP server, JNDI reports it as an javax.naming.NamingException (or subclass) – Hagai Cibulski May 01 '14 at 09:24
  • NamingException a runtime exception, if not then write a generic method in a util class extract the error code from exception, where you are handling exception that place jsut getErrorCode() method. – Asif Bhutto May 01 '14 at 09:30
  • "extract the error code from exception" - that's the question – Hagai Cibulski May 01 '14 at 10:05