1// OpenLDAP: pkg/ldap/contrib/ldapc++/src/LDAPException.cpp,v 1.8.2.5 2008/04/14 23:09:26 quanah Exp
2/*
3 * Copyright 2000, 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
14using namespace std;
15
16LDAPException::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
24LDAPException::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
49LDAPException::~LDAPException() throw()
50{
51}
52
53int LDAPException::getResultCode() const throw()
54{
55	return m_res_code;
56}
57
58const string& LDAPException::getResultMsg() const throw()
59{
60	return m_res_string;
61}
62
63const string& LDAPException::getServerMsg() const throw()
64{
65    return m_err_string;
66}
67
68const char* LDAPException::what() const throw()
69{
70    return this->m_res_string.c_str();
71}
72
73ostream& 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
83LDAPReferralException::LDAPReferralException(const LDAPUrlList& urls) throw()
84        : LDAPException(LDAPResult::REFERRAL) , m_urlList(urls)
85{
86}
87
88LDAPReferralException::~LDAPReferralException() throw()
89{
90}
91
92const LDAPUrlList& LDAPReferralException::getUrls() throw()
93{
94    return m_urlList;
95}
96
97