Back to index

openldap  2.4.31
LDAPException.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 #include <ldap.h>
00008 #include "config.h"
00009 #include "LDAPException.h"
00010 
00011 #include "LDAPAsynConnection.h"
00012 #include "LDAPResult.h"
00013 
00014 using namespace std;
00015 
00016 LDAPException::LDAPException(int res_code, const string& err_string) throw()
00017     : std::runtime_error(err_string)
00018 {
00019        m_res_code=res_code;
00020        m_res_string=string(ldap_err2string(res_code));
00021     m_err_string=err_string;
00022 }
00023 
00024 LDAPException::LDAPException(const LDAPAsynConnection *lc) throw()
00025     : std::runtime_error("")
00026 {
00027     LDAP *l = lc->getSessionHandle();
00028     ldap_get_option(l,LDAP_OPT_RESULT_CODE,&m_res_code);
00029     const char *res_cstring = ldap_err2string(m_res_code);
00030     if ( res_cstring ) {
00031         m_res_string = string(res_cstring);
00032     } else {
00033         m_res_string = "";
00034     }
00035     const char* err_string;
00036 
00037 #ifdef LDAP_OPT_DIAGNOSTIC_MESSAGE
00038     ldap_get_option(l,LDAP_OPT_DIAGNOSTIC_MESSAGE ,&err_string);
00039 #else
00040     ldap_get_option(l,LDAP_OPT_ERROR_STRING,&err_string);
00041 #endif
00042     if ( err_string ) {
00043         m_err_string = string(err_string);
00044     } else {
00045         m_err_string = "";
00046     }
00047 }
00048 
00049 LDAPException::~LDAPException() throw()
00050 {
00051 }
00052 
00053 int LDAPException::getResultCode() const throw()
00054 {
00055        return m_res_code;
00056 }
00057 
00058 const string& LDAPException::getResultMsg() const throw()
00059 {
00060        return m_res_string;
00061 }
00062 
00063 const string& LDAPException::getServerMsg() const throw()
00064 {
00065     return m_err_string;
00066 }
00067 
00068 const char* LDAPException::what() const throw()
00069 {
00070     return this->m_res_string.c_str(); 
00071 }
00072 
00073 ostream& operator << (ostream& s, LDAPException e) throw()
00074 {
00075        s << "Error " << e.m_res_code << ": " << e.m_res_string;
00076        if (!e.m_err_string.empty()) {
00077               s << endl <<  "additional info: " << e.m_err_string ;
00078        }
00079        return s;
00080 }
00081 
00082 
00083 LDAPReferralException::LDAPReferralException(const LDAPUrlList& urls) throw() 
00084         : LDAPException(LDAPResult::REFERRAL) , m_urlList(urls)
00085 {
00086 }
00087 
00088 LDAPReferralException::~LDAPReferralException() throw()
00089 {
00090 }
00091 
00092 const LDAPUrlList& LDAPReferralException::getUrls() throw()
00093 {
00094     return m_urlList;
00095 }
00096