summaryrefslogtreecommitdiffstats
path: root/contrib/ldapc++/src/LDAPException.cpp
blob: 3ae7f9097dd32f423282830eaee8b7642b528dc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// $OpenLDAP$
/*
 * Copyright 2000-2021 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

#include <ldap.h>
#include "config.h"
#include "LDAPException.h"

#include "LDAPAsynConnection.h"
#include "LDAPResult.h"

using namespace std;

LDAPException::LDAPException(int res_code, const string& err_string) throw()
    : std::runtime_error(err_string)
{
	m_res_code=res_code;
	m_res_string=string(ldap_err2string(res_code));
    m_err_string=err_string;
}

LDAPException::LDAPException(const LDAPAsynConnection *lc) throw()
    : std::runtime_error("")
{
    LDAP *l = lc->getSessionHandle();
    ldap_get_option(l,LDAP_OPT_RESULT_CODE,&m_res_code);
    const char *res_cstring = ldap_err2string(m_res_code);
    if ( res_cstring ) {
        m_res_string = string(res_cstring);
    } else {
        m_res_string = "";
    }
    const char* err_string;

#ifdef LDAP_OPT_DIAGNOSTIC_MESSAGE
    ldap_get_option(l,LDAP_OPT_DIAGNOSTIC_MESSAGE ,&err_string);
#else
    ldap_get_option(l,LDAP_OPT_ERROR_STRING,&err_string);
#endif
    if ( err_string ) {
        m_err_string = string(err_string);
    } else {
        m_err_string = "";
    }
}

LDAPException::~LDAPException() throw()
{
}

int LDAPException::getResultCode() const throw()
{
	return m_res_code;
}

const string& LDAPException::getResultMsg() const throw()
{
	return m_res_string;
}

const string& LDAPException::getServerMsg() const throw()
{
    return m_err_string;
}

const char* LDAPException::what() const throw()
{
    return this->m_res_string.c_str(); 
}

ostream& operator << (ostream& s, LDAPException e) throw()
{
	s << "Error " << e.m_res_code << ": " << e.m_res_string;
	if (!e.m_err_string.empty()) {
		s << endl <<  "additional info: " << e.m_err_string ;
	}
	return s;
}


LDAPReferralException::LDAPReferralException(const LDAPUrlList& urls) throw() 
        : LDAPException(LDAPResult::REFERRAL) , m_urlList(urls)
{
}

LDAPReferralException::~LDAPReferralException() throw()
{
}

const LDAPUrlList& LDAPReferralException::getUrls() throw()
{
    return m_urlList;
}