COM-37: Improve COM error messages: include PEP_STATUS or other details.
authorMarkus Schaber <markus@pep-security.net>
Tue, 25 Oct 2016 22:12:19 +0200
changeset 1895361d677afdb
parent 188 cf4a4e973ab2
child 190 8d1c4f057dea
COM-37: Improve COM error messages: include PEP_STATUS or other details.

This will help to debug this and similar problems in the future.
CpEpEngine.cpp
CpEpEngine.h
     1.1 --- a/CpEpEngine.cpp	Sun Oct 23 23:27:46 2016 +0200
     1.2 +++ b/CpEpEngine.cpp	Tue Oct 25 22:12:19 2016 +0200
     1.3 @@ -23,7 +23,8 @@
     1.4  	return S_FALSE;
     1.5  }
     1.6  
     1.7 -#define FAIL(msg) error(msg)
     1.8 +// The second argument is optional, and currently supports PEP_STATUS.
     1.9 +#define FAIL(msg, ...) error(msg, __VA_ARGS__)
    1.10  
    1.11  STDMETHODIMP CpEpEngine::VerboseLogging(VARIANT_BOOL enable)
    1.12  {
    1.13 @@ -61,7 +62,7 @@
    1.14          return E_OUTOFMEMORY;
    1.15  
    1.16      if (status != ::PEP_STATUS_OK)
    1.17 -        return FAIL(L"export_key");
    1.18 +        return FAIL(L"export_key", status);
    1.19  
    1.20      _bstr_t b_key_data(utf16_string(_key_data).c_str());
    1.21      pEp_free(_key_data);
    1.22 @@ -102,7 +103,7 @@
    1.23  	PEP_STATUS _status = ::log_event(get_session(), _title.c_str(), _entity.c_str(), _description.c_str(), _comment.c_str());
    1.24  	assert(_status == PEP_STATUS_OK);
    1.25  	if (_status != PEP_STATUS_OK)
    1.26 -		return FAIL(L"log_event");
    1.27 +		return FAIL(L"log_event", _status);
    1.28  	else
    1.29  		return S_OK;
    1.30  }
    1.31 @@ -153,7 +154,7 @@
    1.32  
    1.33  	if (_words == NULL) {
    1.34  		*words = NULL;
    1.35 -		return FAIL(L"TrustWords");
    1.36 +		return FAIL(L"TrustWords: _words == NULL", status);
    1.37  	}
    1.38  	else {
    1.39  		*words = utf16_bstr(_words);
    1.40 @@ -175,8 +176,10 @@
    1.41  	assert(status == PEP_STATUS_OK);
    1.42  	if (status == PEP_OUT_OF_MEMORY)
    1.43  		return E_OUTOFMEMORY;
    1.44 -	if (status != PEP_STATUS_OK || _log == NULL)
    1.45 -		return FAIL(L"GetCrashdumpLog");
    1.46 +	if (status != PEP_STATUS_OK)
    1.47 +		return FAIL(L"GetCrashdumpLog", status);
    1.48 +    if (_log == NULL)
    1.49 +        return FAIL(L"GetCrashdumpLog: _log == NULL");
    1.50  
    1.51  	*log = utf16_bstr(_log);
    1.52  	pEp_free(_log);
    1.53 @@ -193,7 +196,7 @@
    1.54  	const char *_engine_version = ::get_engine_version();
    1.55  
    1.56  	if (_engine_version == NULL)
    1.57 -		return FAIL(L"GetEngineVersion");
    1.58 +		return FAIL(L"GetEngineVersion: _engine_version == NULL");
    1.59  
    1.60  	*engine_version = utf16_bstr(_engine_version);
    1.61  
    1.62 @@ -212,8 +215,10 @@
    1.63  	assert(status == PEP_STATUS_OK);
    1.64  	if (status == PEP_OUT_OF_MEMORY)
    1.65  		return E_OUTOFMEMORY;
    1.66 -	if (status != PEP_STATUS_OK || _languages == NULL)
    1.67 -		return FAIL(L"GetLanguageList");
    1.68 +    if (status != PEP_STATUS_OK)
    1.69 +        return FAIL(L"GetLanguageList", status);
    1.70 +	if (_languages == NULL)
    1.71 +		return FAIL(L"GetLanguageList: _languages == NULL");
    1.72  
    1.73  	*languages = utf16_bstr(_languages);
    1.74  	pEp_free(_languages);
    1.75 @@ -284,7 +289,7 @@
    1.76  		if (status == PEP_OUT_OF_MEMORY)
    1.77  			return E_OUTOFMEMORY;
    1.78  		else
    1.79 -			return FAIL(L"myself");
    1.80 +			return FAIL(L"myself", status);
    1.81  	}
    1.82  }
    1.83  
    1.84 @@ -314,7 +319,7 @@
    1.85  		if (status == PEP_OUT_OF_MEMORY)
    1.86  			return E_OUTOFMEMORY;
    1.87  		else
    1.88 -			return FAIL(L"UpdateIdentity");
    1.89 +			return FAIL(L"UpdateIdentity", status);
    1.90  	}
    1.91  }
    1.92  
    1.93 @@ -344,7 +349,7 @@
    1.94  		return FAIL(L"key not found");
    1.95  
    1.96  	if (status != ::PEP_STATUS_OK)
    1.97 -		return FAIL(L"cannot revoke compromized key");
    1.98 +		return FAIL(L"cannot revoke compromized key", status);
    1.99  
   1.100  	return S_OK;
   1.101  }
   1.102 @@ -375,7 +380,7 @@
   1.103  		return FAIL(L"key not found");
   1.104  
   1.105  	if (status != ::PEP_STATUS_OK)
   1.106 -		return FAIL(L"cannot reset trust");
   1.107 +		return FAIL(L"cannot reset trust", status);
   1.108  
   1.109  	return S_OK;
   1.110  }
   1.111 @@ -480,7 +485,7 @@
   1.112  	PEP_STATUS status = ::blacklist_add(get_session(), _fpr.c_str());
   1.113  	assert(status == PEP_STATUS_OK);
   1.114  	if (status != PEP_STATUS_OK)
   1.115 -		return FAIL(L"blacklist_add failed in pEp engine");
   1.116 +		return FAIL(L"blacklist_add failed in pEp engine", status);
   1.117  
   1.118  	return S_OK;
   1.119  }
   1.120 @@ -493,7 +498,7 @@
   1.121  	PEP_STATUS status = ::blacklist_delete(get_session(), _fpr.c_str());
   1.122  	assert(status == PEP_STATUS_OK);
   1.123  	if (status != PEP_STATUS_OK)
   1.124 -		return FAIL(L"blacklist_delete failed in pEp engine");
   1.125 +		return FAIL(L"blacklist_delete failed in pEp engine", status);
   1.126  
   1.127  	return S_OK;
   1.128  }
   1.129 @@ -508,7 +513,7 @@
   1.130  	PEP_STATUS status = ::blacklist_is_listed(get_session(), _fpr.c_str(), &result);
   1.131  	assert(status == PEP_STATUS_OK);
   1.132  	if (status != PEP_STATUS_OK)
   1.133 -		return FAIL(L"blacklist_is_listed failed in pEp engine");
   1.134 +		return FAIL(L"blacklist_is_listed failed in pEp engine", status);
   1.135  
   1.136  	*listed = result ? VARIANT_TRUE : VARIANT_FALSE;
   1.137  	return S_OK;
   1.138 @@ -522,7 +527,7 @@
   1.139  	PEP_STATUS status = ::blacklist_retrieve(get_session(), &_blacklist);
   1.140  	assert(status == PEP_STATUS_OK);
   1.141  	if (status != PEP_STATUS_OK)
   1.142 -		return FAIL(L"blacklist_retrieve failed in pEp engine");
   1.143 +		return FAIL(L"blacklist_retrieve failed in pEp engine", status);
   1.144  	assert(_blacklist);
   1.145  
   1.146  	*blacklist = string_array(_blacklist);
   1.147 @@ -552,6 +557,21 @@
   1.148  	return E_FAIL;
   1.149  }
   1.150  
   1.151 +HRESULT CpEpEngine::error(_bstr_t msg, PEP_STATUS status)
   1.152 +{
   1.153 +    std::stringstream stream;
   1.154 +    stream << msg;
   1.155 +    stream << ": ";
   1.156 +    stream << std::hex << status;
   1.157 +    
   1.158 +    error(stream.str().c_str());
   1.159 +
   1.160 +    if (status == ::PEP_OUT_OF_MEMORY)
   1.161 +        return E_OUTOFMEMORY;
   1.162 +
   1.163 +    return E_FAIL;
   1.164 +}
   1.165 +
   1.166  STDMETHODIMP CpEpEngine::EncryptMessage(TextMessage * src, TextMessage * dst, SAFEARRAY * extra, pEpEncryptFlags flags)
   1.167  {
   1.168  	assert(src);
   1.169 @@ -627,7 +647,7 @@
   1.170  	PEP_rating _rating;
   1.171  	PEP_STATUS status = ::outgoing_message_rating(get_session(), _msg, &_rating);
   1.172  	if (status != PEP_STATUS_OK)
   1.173 -		return FAIL(L"cannot get message rating");
   1.174 +		return FAIL(L"cannot get message rating", status);
   1.175  
   1.176  	*pVal = (pEpRating)_rating;
   1.177  	return S_OK;
   1.178 @@ -654,7 +674,7 @@
   1.179  	PEP_STATUS status = ::identity_rating(get_session(), _ident, &_rating);
   1.180  	free_identity(_ident);
   1.181  	if (status != PEP_STATUS_OK)
   1.182 -		return FAIL(L"cannot get message color");
   1.183 +		return FAIL(L"cannot get message color", status);
   1.184  
   1.185  	*pVal = (pEpRating)_rating;
   1.186  	return S_OK;
   1.187 @@ -718,7 +738,7 @@
   1.188  	if (status == PEP_OUT_OF_MEMORY)
   1.189  		return E_OUTOFMEMORY;
   1.190  	else if (status != PEP_STATUS_OK)
   1.191 -		return FAIL(L"failure while executing TrustPersonalKey()");
   1.192 +		return FAIL(L"failure while executing TrustPersonalKey()", status);
   1.193  
   1.194  	return S_OK;
   1.195  }
   1.196 @@ -933,7 +953,7 @@
   1.197  		return E_OUTOFMEMORY;
   1.198  
   1.199  	if (status != ::PEP_STATUS_OK)
   1.200 -		return FAIL(L"OpenPGP_list_keyinfo");
   1.201 +		return FAIL(L"OpenPGP_list_keyinfo", status);
   1.202  
   1.203  	if (_keyinfo_list && _keyinfo_list->value) {
   1.204  		::opt_field_array_from_C(_keyinfo_list, keyinfo_list);
     2.1 --- a/CpEpEngine.h	Sun Oct 23 23:27:46 2016 +0200
     2.2 +++ b/CpEpEngine.h	Tue Oct 25 22:12:19 2016 +0200
     2.3 @@ -107,6 +107,7 @@
     2.4  
     2.5  
     2.6      HRESULT error(_bstr_t msg);
     2.7 +    HRESULT error(_bstr_t msg, PEP_STATUS errorcode);
     2.8  
     2.9      void verbose(string text)
    2.10      {