]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPException.cpp
initial support for SASL
[openldap] / contrib / ldapc++ / src / LDAPException.cpp
1 /*
2  * Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6
7
8 #include <ldap.h>
9 #include "config.h"
10 #include "LDAPException.h"
11 #include "LDAPReferralException.h"
12
13 #include "LDAPAsynConnection.h"
14
15 using namespace std;
16
17 LDAPException::LDAPException(int res_code, const string& err_string){
18         m_res_code=res_code;
19         m_res_string=string(ldap_err2string(res_code));
20     m_err_string=err_string;
21 }
22
23 LDAPException::LDAPException(const LDAPAsynConnection *lc){
24     LDAP *l = lc->getSessionHandle();
25     ldap_get_option(l,LDAP_OPT_RESULT_CODE,&m_res_code);
26     const char *res_cstring = ldap_err2string(m_res_code);
27     if ( res_cstring ) {
28         m_res_string = string(res_cstring);
29     } else {
30         m_res_string = "";
31     }
32     const char* err_string;
33
34 #ifdef LDAP_OPT_DIAGNOSTIC_MESSAGE
35     ldap_get_option(l,LDAP_OPT_DIAGNOSTIC_MESSAGE ,&err_string);
36 #else
37     ldap_get_option(l,LDAP_OPT_ERROR_STRING,&err_string);
38 #endif
39     if ( err_string ) {
40         m_err_string = string(err_string);
41     } else {
42         m_err_string = "";
43     }
44 }
45
46 LDAPException::~LDAPException(){
47 }
48
49 int LDAPException::getResultCode() const{
50         return m_res_code;
51 }
52
53 const string& LDAPException::getResultMsg() const{
54         return m_res_string;
55 }
56
57 const string& LDAPException::getServerMsg() const{
58     return m_err_string;
59 }
60
61 ostream& operator << (ostream& s, LDAPException e){
62         s << "Error " << e.m_res_code << ": " << e.m_res_string;
63         if (!e.m_err_string.empty()) {
64                 s << endl <<  "additional info: " << e.m_err_string ;
65         }
66         return s;
67 }
68