Remove dynamic exceptions
[anna.git] / source / ldap / ResultCode.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <time.h>
10 #include <ldap.h>
11
12 #include <anna/core/functions.hpp>
13
14 #include <anna/ldap/ResultCode.hpp>
15 #include <anna/ldap/Session.hpp>
16
17 using namespace std;
18
19 #define DoErrorCode(methodName,macroName) \
20    bool anna::ldap::ResultCode::methodName () const \
21        \
22    { \
23       return a_value == (macroName); \
24    }
25
26 DoErrorCode(isOperationsError, LDAP_OPERATIONS_ERROR)
27 DoErrorCode(isInvalidCredential, LDAP_INVALID_CREDENTIALS)
28 DoErrorCode(isProtocolError, LDAP_PROTOCOL_ERROR)
29 DoErrorCode(isTimeLimitExceeded, LDAP_TIMELIMIT_EXCEEDED)
30 DoErrorCode(isSizeLimitExceeded, LDAP_SIZELIMIT_EXCEEDED)
31 DoErrorCode(isAuthMethodNotSupported, LDAP_AUTH_METHOD_NOT_SUPPORTED)
32 DoErrorCode(isStrongAuthRequired, LDAP_STRONG_AUTH_REQUIRED)
33 DoErrorCode(isSASLBindInProgress, LDAP_SASL_BIND_IN_PROGRESS)
34 DoErrorCode(isTimeout, LDAP_TIMEOUT)
35 DoErrorCode(isUnavailable, LDAP_UNAVAILABLE)
36 DoErrorCode(isServerDown, LDAP_SERVER_DOWN)
37 DoErrorCode(isLocalError, LDAP_LOCAL_ERROR)
38 DoErrorCode(isDecodingError, LDAP_DECODING_ERROR)
39 DoErrorCode(isFilterError, LDAP_FILTER_ERROR)
40 DoErrorCode(isConnectError, LDAP_CONNECT_ERROR)
41
42 #define DoErrorRange(methodName,rangeName) \
43    bool anna::ldap::ResultCode::methodName () const \
44        \
45    { \
46       return rangeName (a_value) != 0; \
47    }
48
49 DoErrorRange(isAttrError, LDAP_ATTR_ERROR)
50 DoErrorRange(isNameError, LDAP_NAME_ERROR)
51 DoErrorRange(isSecurityError, LDAP_SECURITY_ERROR)
52 DoErrorRange(isServiceError, LDAP_SERVICE_ERROR)
53
54 anna::ldap::ResultCode::ResultCode(const int ldap_method_result) :
55   a_value(ldap_method_result) {
56   if(ldap_method_result != LDAP_SUCCESS) {
57     const char* text = ldap_err2string(ldap_method_result);
58     a_text = (text == NULL) ? "<undefined>" : text;
59   }
60 }
61
62
63 /**
64  * Al invocar a un método que devuelva el resultado y además haya que pasar una variable
65  * donde contener el error pasaremos ldap::ResultCode::extraError, para que vuelve ahí el
66  * código de error.
67  + Una vez hecho eso, el código de error "real" será el máximo de los dos. Por es posible
68  * que el método invicado devuelva 0, pero indique el código de error el extraError.
69  */
70 anna::ldap::ResultCode& anna::ldap::ResultCode::operator= (const int ldap_method_result)
71 {
72   if((a_value = ldap_method_result) != LDAP_SUCCESS) {
73     const char* text = ldap_err2string(ldap_method_result);
74     a_text = (text == NULL) ? "<undefined>" : text;
75   }
76
77   return *this;
78 }
79
80 void anna::ldap::ResultCode::setValue(const int ldap_method_result, const int ldap_method_error)
81 {
82   if(ldap_method_result != LDAP_SUCCESS)
83     operator= (ldap_method_result);
84   else
85     operator= (ldap_method_error);
86 }
87
88 const string  anna::ldap::ResultCode::asString() const
89 {
90   string result(functions::asText("ldap::ResultCode { Value: ", a_value));
91
92   if(a_text.empty() == false) {
93     result += " | Text: ";
94     result += a_text;
95   }
96
97   return result += " }";
98 }
99
100 bool anna::ldap::ResultCode::extractResultCode(const Session* session)
101 {
102   LDAP* handle = (LDAP*)((Session*) session)->getLDAP();
103   int result = -1;
104
105   if(ldap_get_option(handle, LDAP_OPT_RESULT_CODE, &result) == LDAP_OPT_SUCCESS) {
106     operator= (result);
107     return true;
108   }
109
110   return false;
111 }