Source

swish_comet / include / comet / error.h

Full commit
  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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/** \file
  * Provide COM Error support.
  */
/*
 * Copyright Š 2000, 2001 Sofus Mortensen
 *
 * This material is provided "as is", with absolutely no warranty
 * expressed or implied. Any use is at your own risk. Permission to
 * use or copy this software for any purpose is hereby granted without
 * fee, provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is
 * granted, provided the above notices are retained, and a notice that
 * the code was modified is included with the above copyright notice.
 *
 * This header is part of comet.
 * http://www.lambdasoft.dk/comet
 */

#ifndef COMET_ERROR_H
#define COMET_ERROR_H

#include <comet/config.h>

#include <stdexcept>
#include <string>

#include <comet/error_fwd.h>
#include <comet/bstr.h>
#include <comet/ptr.h>
#include <comet/uuid_fwd.h>

#pragma warning(push)
#pragma warning(disable : 4290)

namespace comet {

	namespace impl {

		inline com_ptr<IErrorInfo> GetErrorInfo() throw()
		{
			IErrorInfo* ei = 0;
			::GetErrorInfo(0, &ei);
			return com_ptr<IErrorInfo>(auto_attach(ei));
		}

		inline com_ptr<ICreateErrorInfo> CreateErrorInfo() throw()
		{
			ICreateErrorInfo* cei = 0;
			::CreateErrorInfo(&cei);
			return com_ptr<ICreateErrorInfo>(auto_attach(cei));
		}

		template<typename Itf> inline bool supports_ErrorInfo(Itf* p)
		{
			com_ptr<ISupportErrorInfo> q = com_cast(com_ptr<Itf>(p));
			if (q == 0) return false;
			return S_OK == q->InterfaceSupportsErrorInfo(uuidof<Itf>());
		}

	}
	

	/** \class com_error  error.h comet/error.h
	  * COM error.
	  */
	class com_error : public std::runtime_error
	{
	public:
		//! Construct com_error from HRESULT.
		/*!
			\param hr
				HRESULT value of error.
		*/
		explicit com_error(HRESULT hr) : hr_(hr), std::runtime_error("")
		{}

		//! Construct com_error from HRESULT and textual description.
		/*!
			\param msg
				Description of error.
			\param hr
				HRESULT value of error.
		*/
		explicit com_error(const bstr_t& msg, HRESULT hr = E_FAIL) : hr_(hr), std::runtime_error("")
		{
			com_ptr<ICreateErrorInfo> cei(impl::CreateErrorInfo());
			if ( !cei.is_null() ) {
				try {
					cei->SetDescription(msg.in());
					ei_ = com_ptr<IErrorInfo>( com_cast(cei)) ;
				} catch (std::bad_alloc&)
				{}
			}
		}
		//! Construct com_error from HRESULT, textual description, source, iid, help.
		/*!
			\param msg          Description of error.
			\param hr           HRESULT value of error.
			\param src          Description of source line
			\param iid          Interface the error was on
			\param helpFile     Name of help file
			\param helpContext  Name of help Context
		*/
		explicit com_error(const bstr_t &msg, HRESULT hr, const bstr_t &src, const uuid_t &iid = uuid_t(),
				const bstr_t &helpFile=bstr_t(), DWORD helpContext = -1)
			: hr_(hr), std::runtime_error("")
		{
			com_ptr<ICreateErrorInfo> cei(impl::CreateErrorInfo());
			if ( ! cei.is_null() )
			{
				try {
					cei->SetDescription(msg.in());
					if (!src.is_null() )
						cei->SetSource( src.in() );
					if (iid != uuid_t())
						cei->SetGUID( iid );
					if (!helpFile.is_null())
					{
						cei->SetHelpFile( helpFile.in() );
						cei->SetHelpContext( helpContext );
					}
					ei_ = com_ptr<IErrorInfo>( com_cast(cei)) ;
				} catch (std::bad_alloc&)
				{}
			}
		}

		/// Construct with an error-info and hresult.
		explicit com_error(HRESULT hr, const com_ptr<IErrorInfo>& ei)
			: hr_(hr), ei_(ei), std::runtime_error("")
		{}

	public:
		//! Return a string with a description of the error
		/*!
			what() uses Description from IErrorInfo if such is present, otherwise FormatMessage is used
			to create a description of the HRESULT error value.

			\retval
				A const char* string with a textual description of the error.
		*/
		const char* what() const throw()
		{
			try {
				if (what_.empty()) {
					what_ = s_str();
				}
			} catch (std::bad_alloc&) {
				return 0;
			}

			return what_.c_str();
		}
		/// Returns a std::string with a description of the error.
		std::string s_str() const
		{
			std::string ret;
			get_str(ret);
			return ret;
		}
		/// Returns a std::wstring with a description of the error.
		std::wstring w_str() const
		{
			std::wstring ret;
			get_str(ret);
			return ret;
		}
		/// Returns a tstring with a description of the error.
		tstring t_str() const
		{
			tstring ret;
			get_str(ret);
			return ret;
		}

	private:
		void get_str(std::string& ret) const
		{
			if (ei_.is_null() == false)
			{
				bstr_t bs;
				if (SUCCEEDED(ei_->GetDescription(bs.out())) && !bs.is_empty())
				{
					ret= bs.s_str();
					return;
				}
			}

			char* lpMsgBuf;
			if (FormatMessageA(
				FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
				FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr_,
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
				reinterpret_cast<char*>(&lpMsgBuf), 0, NULL))
			{
				char* lpEnd = lpMsgBuf;
				while (*lpEnd != '\0')
					lpEnd++;

				while (lpEnd > lpMsgBuf && // Trim trailing newlines
					(*(lpEnd - 1) == '\n' || *(lpEnd - 1) == '\r'))
					lpEnd--;

				ret.assign(lpMsgBuf, lpEnd - lpMsgBuf);
				LocalFree(lpMsgBuf);
			}
		}

		void get_str(std::wstring& ret) const
		{
			if (ei_.is_null() == false)
			{
				bstr_t bs;
				if (SUCCEEDED(ei_->GetDescription(bs.out())) && !bs.is_empty())
				{
					 ret = bs.w_str();
					 return;
				}
			}

			wchar_t* lpMsgBuf;
			if (FormatMessageW(
				FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
				FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr_,
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
				reinterpret_cast<wchar_t*>(&lpMsgBuf), 0, NULL))
			{
				wchar_t* lpEnd = lpMsgBuf;
				while (*lpEnd != L'\0')
					lpEnd++;

				while (lpEnd > lpMsgBuf && // Trim trailing newlines
					(*(lpEnd - 1) == L'\n' || *(lpEnd - 1) == L'\r'))
					lpEnd--;

				ret.assign(lpMsgBuf, lpEnd - lpMsgBuf);
				LocalFree(lpMsgBuf);
			}
		}

	public:

		/** Return the HRESULT for the error.
		  */
		HRESULT hr() const throw()
		{
			return hr_;
		}

		/// \name wrappers for IErrorInfo
		//@{

		/// Description of the error.
		bstr_t description() const
		{
			bstr_t rv;
			if (!ei_.is_null()) ei_->GetDescription( rv.out() ) | raise_exception;
			return rv;
		}

		/// The error source description.
		bstr_t source() const
		{
			bstr_t rv;
			if (!ei_.is_null()) ei_->GetSource( rv.out() ) | raise_exception;
			return rv;
		}

		/// Interface IID.
		GUID guid() const
		{
			GUID rv;
			if (!ei_.is_null()) ei_->GetGUID( &rv ) | raise_exception;
			else ZeroMemory(&rv, sizeof(rv));
			return rv;
		}

		/// Helpfile name.
		bstr_t help_file() const
		{
			bstr_t rv;
			if (!ei_.is_null()) ei_->GetHelpFile( rv.out() ) | raise_exception;
			return rv;
		}


		/// Help conext.
		DWORD help_context() const
		{
			DWORD rv = 0;
			if (!ei_.is_null()) ei_->GetHelpContext( &rv ) | raise_exception;
			return rv;
		}
		//@}

		/// Return the error-info object.
		com_ptr<IErrorInfo> get_ei() const
		{
			return ei_;
		}

	private:
		mutable std::string what_;
		com_ptr<IErrorInfo> ei_;
		HRESULT hr_;
	};


	namespace impl {

		inline HRESULT  return_com_error(HRESULT hr, const bstr_t &desc, const bstr_t &src = auto_attach(BSTR(NULL)),
				const uuid_t &iid=CLSID_NULL, const bstr_t &helpFile=bstr_t(), DWORD helpContext = -1)
		{
			com_ptr<ICreateErrorInfo> cei(impl::CreateErrorInfo());
			if (cei.is_null() == false) {
				try {
					cei->SetDescription(desc.in());
					if (!src.is_null() )
						cei->SetSource( src.in() );
					if (iid != uuid_t())
						cei->SetGUID( iid );
					if (!helpFile.is_null())
					{
						cei->SetHelpFile( helpFile.in() );
						cei->SetHelpContext( helpContext );
					}
					com_ptr<IErrorInfo> ei = com_cast(cei);
					::SetErrorInfo(0, ei.in());
				} catch (std::bad_alloc&)
				{}
			}
			return hr;
		}
		inline HRESULT  return_com_error(const std::exception& err, const bstr_t &src = auto_attach(BSTR(NULL)),
				const uuid_t &iid=CLSID_NULL, const bstr_t &helpFile=bstr_t(), DWORD helpContext = -1)
		{
			return return_com_error( E_FAIL, bstr_t(err.what()),src,iid,helpFile, helpContext );
		}
		inline HRESULT return_com_error(const com_error& err, const bstr_t &src = bstr_t(), const uuid_t &iid = CLSID_NULL) throw()
		{
			const bstr_t &cursrc =err.source();
			const uuid_t &curiid =err.guid();
			// Return error info with more info.
			return return_com_error( err.hr(), err.description(), cursrc.is_null()?src:cursrc,
					curiid.is_null()?iid:curiid, err.help_file(), err.help_context());
		}


		inline void throw_com_error_(HRESULT hr, const com_ptr<IErrorInfo>& ei)
		{
			throw_error_handler<true>::throw_error(hr, ei);
		}

		// Raising an HRESULT with a message
		inline void raise_exception_t::operator()(const bstr_t& msg, HRESULT hr/* = E_FAIL*/) const
		{
			throw com_error(msg, hr);
		}

		// Raising an HRESULT
		inline void raise_exception_t::operator()(HRESULT hr) const
		{
			throw com_error(hr);
		}

	} // namespace impl

        /*! \addtogroup ErrorHandling
	 */
	//@{

	/** Throw COM error using com_error, using HRESULT and IErrorInfo.
	  */
	template<typename Itf> inline void throw_com_error(Itf* p, HRESULT hr)
	{
		if (impl::supports_ErrorInfo(p))
		{
			com_ptr<IErrorInfo> ei = impl::GetErrorInfo();
			if (ei.is_null() == false) impl::throw_com_error_(hr, ei);
		}
		throw com_error(hr);
	}

	template<bool OVERRIDE>
	inline void throw_error_handler<OVERRIDE>::throw_error(HRESULT hr, const com_ptr<IErrorInfo> &ei)
	{
		throw com_error(hr, ei);
	}

	/**
	 * Construct exception for a method called on a raw COM interface.
	 *
	 * This method aims to imbue the com_error with as much information about
	 * the failure as possible:
	 * - If the interface supports IErrorInfo, the information is taken from
	 *   the last ErrorInfo set on the current thread.
	 * - If not, the HRESULT alone determines the message.
	 *
	 * Use this constructor immediately after an interface returns a
	 * failure code before any other code can call SetErrorInfo and
	 * overwrite the error.
	 *
	 * \param failure_source  Interface whose method returned a failure code.
	 * \param hr              HRESULT value of error.
	 *
	 * \todo  Can we add an optional user-defined message to this?
	 */
	template<typename Itf>
	com_error com_error_from_interface(Itf* failure_source, HRESULT hr)
	{
		if (impl::supports_ErrorInfo(failure_source))
			return com_error(hr, impl::GetErrorInfo());
		else
			return com_error(hr);
	}

	template<typename Itf>
	com_error com_error_from_interface(
		com_ptr<Itf> failure_source, HRESULT hr)
	{
		return com_error_from_interface(failure_source.get(), hr);
	}

	//@}
}

#pragma warning(pop)

#endif