xref: /netbsd-src/external/bsd/openldap/dist/contrib/ldapc++/src/LDAPException.cpp (revision e670fd5c413e99c2f6a37901bb21c537fcd322d2)
1 // $OpenLDAP$
2 /*
3  * Copyright 2000-2021 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 
7 #include <ldap.h>
8 #include "config.h"
9 #include "LDAPException.h"
10 
11 #include "LDAPAsynConnection.h"
12 #include "LDAPResult.h"
13 
14 using namespace std;
15 
LDAPException(int res_code,const string & err_string)16 LDAPException::LDAPException(int res_code, const string& err_string) throw()
17     : std::runtime_error(err_string)
18 {
19 	m_res_code=res_code;
20 	m_res_string=string(ldap_err2string(res_code));
21     m_err_string=err_string;
22 }
23 
LDAPException(const LDAPAsynConnection * lc)24 LDAPException::LDAPException(const LDAPAsynConnection *lc) throw()
25     : std::runtime_error("")
26 {
27     LDAP *l = lc->getSessionHandle();
28     ldap_get_option(l,LDAP_OPT_RESULT_CODE,&m_res_code);
29     const char *res_cstring = ldap_err2string(m_res_code);
30     if ( res_cstring ) {
31         m_res_string = string(res_cstring);
32     } else {
33         m_res_string = "";
34     }
35     const char* err_string;
36 
37 #ifdef LDAP_OPT_DIAGNOSTIC_MESSAGE
38     ldap_get_option(l,LDAP_OPT_DIAGNOSTIC_MESSAGE ,&err_string);
39 #else
40     ldap_get_option(l,LDAP_OPT_ERROR_STRING,&err_string);
41 #endif
42     if ( err_string ) {
43         m_err_string = string(err_string);
44     } else {
45         m_err_string = "";
46     }
47 }
48 
~LDAPException()49 LDAPException::~LDAPException() throw()
50 {
51 }
52 
getResultCode() const53 int LDAPException::getResultCode() const throw()
54 {
55 	return m_res_code;
56 }
57 
getResultMsg() const58 const string& LDAPException::getResultMsg() const throw()
59 {
60 	return m_res_string;
61 }
62 
getServerMsg() const63 const string& LDAPException::getServerMsg() const throw()
64 {
65     return m_err_string;
66 }
67 
what() const68 const char* LDAPException::what() const throw()
69 {
70     return this->m_res_string.c_str();
71 }
72 
operator <<(ostream & s,LDAPException e)73 ostream& operator << (ostream& s, LDAPException e) throw()
74 {
75 	s << "Error " << e.m_res_code << ": " << e.m_res_string;
76 	if (!e.m_err_string.empty()) {
77 		s << endl <<  "additional info: " << e.m_err_string ;
78 	}
79 	return s;
80 }
81 
82 
LDAPReferralException(const LDAPUrlList & urls)83 LDAPReferralException::LDAPReferralException(const LDAPUrlList& urls) throw()
84         : LDAPException(LDAPResult::REFERRAL) , m_urlList(urls)
85 {
86 }
87 
~LDAPReferralException()88 LDAPReferralException::~LDAPReferralException() throw()
89 {
90 }
91 
getUrls()92 const LDAPUrlList& LDAPReferralException::getUrls() throw()
93 {
94     return m_urlList;
95 }
96 
97