VirtualBox

source: vbox/trunk/include/VBox/com/ErrorInfo.h@ 45805

Last change on this file since 45805 was 45805, checked in by vboxsync, 12 years ago

Main: Removed IGuestErrorInfo, added new attribute resultDetail to IVirtualBoxErrorInfo for (optionally) providing more details on the error happened.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer - ErrorInfo class declaration.
3 */
4
5/*
6 * Copyright (C) 2006-2013 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_com_ErrorInfo_h
27#define ___VBox_com_ErrorInfo_h
28
29#include "VBox/com/ptr.h"
30#include "VBox/com/string.h"
31#include "VBox/com/Guid.h"
32#include "VBox/com/assert.h"
33
34struct IProgress;
35struct IVirtualBoxErrorInfo;
36
37namespace com
38{
39
40/**
41 * General discussion:
42 *
43 * In COM all errors are stored on a per thread basis. In general this means
44 * only _one_ active error is possible per thread. A new error will overwrite
45 * the previous one. To prevent this use MultiResult or ErrorInfoKeeper (see
46 * below). The implementations in MSCOM/XPCOM differ slightly, but the details
47 * are handled by this glue code.
48 *
49 * We have different classes which are involved in the error management. I try
50 * to describe them separately to make clear what they are there for.
51 *
52 * ErrorInfo:
53 *
54 * This class is able to retrieve the per thread error and store it into its
55 * member variables. This class can also handle non-VirtualBox errors (like
56 * standard COM errors).
57 *
58 * ProgressErrorInfo:
59 *
60 * This is just a simple wrapper class to get the ErrorInfo stored within an
61 * IProgress object. That is the error which was stored when the progress
62 * object was in use and not an error produced by IProgress itself.
63 *
64 * IVirtualBoxErrorInfo:
65 *
66 * The VirtualBox interface class for accessing error information from Main
67 * clients. This class is also used for storing the error information in the
68 * thread context.
69 *
70 * ErrorInfoKeeper:
71 *
72 * A helper class which stores the current per thread info internally. After
73 * calling methods which may produce other errors it is possible to restore
74 * the previous error and therefore restore the situation before calling the
75 * other methods.
76 *
77 * MultiResult:
78 *
79 * Creating an instance of MultiResult turns error chain saving on. All errors
80 * which follow will be saved in a chain for later access.
81 *
82 * COMErrorInfo (Qt/Gui only):
83 *
84 * The Qt GUI does some additional work for saving errors. Because we create
85 * wrappers for _every_ COM call, it is possible to automatically save the
86 * error info after the execution. This allow some additional info like saving
87 * the callee. Please note that this error info is saved on the client side
88 * and therefore locally to the object instance. See COMBaseWithEI,
89 * COMErrorInfo and the generated COMWrappers.cpp in the GUI.
90 *
91 * Errors itself are set in VirtualBoxBase::setErrorInternal. First a
92 * IVirtualBoxErrorInfo object is created and the given error is saved within.
93 * If MultiResult is active the current per thread error is fetched and
94 * attached to the new created IVirtualBoxErrorInfo object. Next this object is
95 * set as the new per thread error.
96 *
97 * Some general hints:
98 *
99 * - Always use setError, especially when you are working in an asynchronous thread
100 * to indicate an error. Otherwise the error information itself will not make
101 * it into the client.
102 *
103 */
104
105/**
106 * The ErrorInfo class provides a convenient way to retrieve error
107 * information set by the most recent interface method, that was invoked on
108 * the current thread and returned an unsuccessful result code.
109 *
110 * Once the instance of this class is created, the error information for
111 * the current thread is cleared.
112 *
113 * There is no sense to use instances of this class after the last
114 * invoked interface method returns a success.
115 *
116 * The class usage pattern is as follows:
117 * <code>
118 * IFoo *foo;
119 * ...
120 * HRESULT rc = foo->SomeMethod();
121 * if (FAILED(rc)) {
122 * ErrorInfo info(foo);
123 * if (info.isFullAvailable()) {
124 * printf("error message = %ls\n", info.getText().raw());
125 * }
126 * }
127 * </code>
128 *
129 * This class fetches error information using the IErrorInfo interface on
130 * Win32 (MS COM) or the nsIException interface on other platforms (XPCOM),
131 * or the extended IVirtualBoxErrorInfo interface when when it is available
132 * (i.e. a given IErrorInfo or nsIException instance implements it).
133 * Currently, IVirtualBoxErrorInfo is only available for VirtualBox components.
134 *
135 * ErrorInfo::isFullAvailable() and ErrorInfo::isBasicAvailable() determine
136 * what level of error information is available. If #isBasicAvailable()
137 * returns true, it means that only IErrorInfo or nsIException is available as
138 * the source of information (depending on the platform), but not
139 * IVirtualBoxErrorInfo. If #isFullAvailable() returns true, it means that all
140 * three interfaces are available. If both methods return false, no error info
141 * is available at all.
142 *
143 * Here is a table of correspondence between this class methods and
144 * and IErrorInfo/nsIException/IVirtualBoxErrorInfo attributes/methods:
145 *
146 * ErrorInfo IErrorInfo nsIException IVirtualBoxErrorInfo
147 * --------------------------------------------------------------------
148 * getResultCode -- result resultCode
149 * getIID GetGUID -- interfaceID
150 * getComponent GetSource -- component
151 * getText GetDescription message text
152 *
153 * '--' means that this interface does not provide the corresponding portion
154 * of information, therefore it is useless to query it if only
155 * #isBasicAvailable() returns true. As it can be seen, the amount of
156 * information provided at the basic level, depends on the platform
157 * (MS COM or XPCOM).
158 */
159class ErrorInfo
160{
161public:
162
163 /**
164 * Constructs a new, "interfaceless" ErrorInfo instance that takes
165 * the error information possibly set on the current thread by an
166 * interface method of some COM component or by the COM subsystem.
167 *
168 * This constructor is useful, for example, after an unsuccessful attempt
169 * to instantiate (create) a component, so there is no any valid interface
170 * pointer available.
171 */
172 explicit ErrorInfo()
173 : mIsBasicAvailable(false),
174 mIsFullAvailable(false),
175 mResultCode(S_OK),
176 mResultDetail(0),
177 m_pNext(NULL)
178 {
179 init();
180 }
181
182 ErrorInfo(IUnknown *pObj, const GUID &aIID)
183 : mIsBasicAvailable(false),
184 mIsFullAvailable(false),
185 mResultCode(S_OK),
186 mResultDetail(0),
187 m_pNext(NULL)
188 {
189 init(pObj, aIID);
190 }
191
192 /** Specialization for the IVirtualBoxErrorInfo smart pointer */
193 ErrorInfo (const ComPtr <IVirtualBoxErrorInfo> &aPtr)
194 : mIsBasicAvailable (false), mIsFullAvailable (false)
195 , mResultCode (S_OK), mResultDetail(0)
196 { init (aPtr); }
197
198 /**
199 * Constructs a new ErrorInfo instance from the IVirtualBoxErrorInfo
200 * interface pointer. If this pointer is not NULL, both #isFullAvailable()
201 * and #isBasicAvailable() will return |true|.
202 *
203 * @param aInfo pointer to the IVirtualBoxErrorInfo interface that
204 * holds error info to be fetched by this instance
205 */
206 ErrorInfo (IVirtualBoxErrorInfo *aInfo)
207 : mIsBasicAvailable (false), mIsFullAvailable (false)
208 , mResultCode (S_OK), mResultDetail(0)
209 { init (aInfo); }
210
211 ErrorInfo(const ErrorInfo &x)
212 {
213 copyFrom(x);
214 }
215
216 virtual ~ErrorInfo()
217 {
218 cleanup();
219 }
220
221 ErrorInfo& operator=(const ErrorInfo& x)
222 {
223 cleanup();
224 copyFrom(x);
225 return *this;
226 }
227
228 /**
229 * Returns whether basic error info is actually available for the current
230 * thread. If the instance was created from an interface pointer that
231 * supports basic error info and successfully provided it, or if it is an
232 * "interfaceless" instance and there is some error info for the current
233 * thread, the returned value will be true.
234 *
235 * See the class description for details about the basic error info level.
236 *
237 * The appropriate methods of this class provide meaningful info only when
238 * this method returns true (otherwise they simply return NULL-like values).
239 */
240 bool isBasicAvailable() const
241 {
242 return mIsBasicAvailable;
243 }
244
245 /**
246 * Returns whether full error info is actually available for the current
247 * thread. If the instance was created from an interface pointer that
248 * supports full error info and successfully provided it, or if it is an
249 * "interfaceless" instance and there is some error info for the current
250 * thread, the returned value will be true.
251 *
252 * See the class description for details about the full error info level.
253 *
254 * The appropriate methods of this class provide meaningful info only when
255 * this method returns true (otherwise they simply return NULL-like values).
256 */
257 bool isFullAvailable() const
258 {
259 return mIsFullAvailable;
260 }
261
262 /**
263 * Returns the COM result code of the failed operation.
264 */
265 HRESULT getResultCode() const
266 {
267 return mResultCode;
268 }
269
270 /**
271 * Returns the (optional) result detail code of the failed operation.
272 */
273 LONG getResultDetail() const
274 {
275 return mResultDetail;
276 }
277
278 /**
279 * Returns the IID of the interface that defined the error.
280 */
281 const Guid& getInterfaceID() const
282 {
283 return mInterfaceID;
284 }
285
286 /**
287 * Returns the name of the component that generated the error.
288 */
289 const Bstr& getComponent() const
290 {
291 return mComponent;
292 }
293
294 /**
295 * Returns the textual description of the error.
296 */
297 const Bstr& getText() const
298 {
299 return mText;
300 }
301
302 /**
303 * Returns the next error information object or @c NULL if there is none.
304 */
305 const ErrorInfo* getNext() const
306 {
307 return m_pNext;
308 }
309
310 /**
311 * Returns the name of the interface that defined the error
312 */
313 const Bstr& getInterfaceName() const
314 {
315 return mInterfaceName;
316 }
317
318 /**
319 * Returns the IID of the interface that returned the error.
320 *
321 * This method returns a non-null IID only if the instance was created
322 * using #template <class I> ErrorInfo(I *i) or
323 * template <class I> ErrorInfo(const ComPtr<I> &i) constructor.
324 */
325 const Guid& getCalleeIID() const
326 {
327 return mCalleeIID;
328 }
329
330 /**
331 * Returns the name of the interface that returned the error
332 *
333 * This method returns a non-null name only if the instance was created
334 * using #template <class I> ErrorInfo(I *i) or
335 * template <class I> ErrorInfo(const ComPtr<I> &i) constructor.
336 */
337 const Bstr& getCalleeName() const
338 {
339 return mCalleeName;
340 }
341
342 /**
343 * Resets all collected error information. #isBasicAvailable() and
344 * #isFullAvailable will return @c true after this method is called.
345 */
346 void setNull()
347 {
348 cleanup();
349 }
350
351protected:
352
353 ErrorInfo(bool /* aDummy */)
354 : mIsBasicAvailable(false),
355 mIsFullAvailable(false),
356 mResultCode(S_OK),
357 m_pNext(NULL)
358 { }
359
360 void copyFrom(const ErrorInfo &x);
361 void cleanup();
362
363 void init(bool aKeepObj = false);
364 void init(IUnknown *aUnk, const GUID &aIID, bool aKeepObj = false);
365 void init(IVirtualBoxErrorInfo *aInfo);
366
367 bool mIsBasicAvailable : 1;
368 bool mIsFullAvailable : 1;
369
370 HRESULT mResultCode;
371 LONG mResultDetail;
372 Guid mInterfaceID;
373 Bstr mComponent;
374 Bstr mText;
375
376 ErrorInfo *m_pNext;
377
378 Bstr mInterfaceName;
379 Guid mCalleeIID;
380 Bstr mCalleeName;
381
382 ComPtr<IUnknown> mErrorInfo;
383};
384
385/**
386 * A convenience subclass of ErrorInfo that, given an IProgress interface
387 * pointer, reads its errorInfo attribute and uses the returned
388 * IVirtualBoxErrorInfo instance to construct itself.
389 */
390class ProgressErrorInfo : public ErrorInfo
391{
392public:
393
394 /**
395 * Constructs a new instance by fetching error information from the
396 * IProgress interface pointer. If the progress object is not NULL,
397 * its completed attribute is true, resultCode represents a failure,
398 * and the errorInfo attribute returns a valid IVirtualBoxErrorInfo pointer,
399 * both #isFullAvailable() and #isBasicAvailable() will return true.
400 *
401 * @param progress the progress object representing a failed operation
402 */
403 ProgressErrorInfo(IProgress *progress);
404};
405
406/**
407 * A convenience subclass of ErrorInfo that allows to preserve the current
408 * error info. Instances of this class fetch an error info object set on the
409 * current thread and keep a reference to it, which allows to restore it
410 * later using the #restore() method. This is useful to preserve error
411 * information returned by some method for the duration of making another COM
412 * call that may set its own error info and overwrite the existing
413 * one. Preserving and restoring error information makes sense when some
414 * method wants to return error information set by other call as its own
415 * error information while it still needs to make another call before return.
416 *
417 * Instead of calling #restore() explicitly you may let the object destructor
418 * do it for you, if you correctly limit the object's lifetime.
419 *
420 * The usage pattern is:
421 * <code>
422 * rc = foo->method();
423 * if (FAILED(rc))
424 * {
425 * ErrorInfoKeeper eik;
426 * ...
427 * // bar may return error info as well
428 * bar->method();
429 * ...
430 * // no need to call #restore() explicitly here because the eik's
431 * // destructor will restore error info fetched after the failed
432 * // call to foo before returning to the caller
433 * return rc;
434 * }
435 * </code>
436 */
437class ErrorInfoKeeper : public ErrorInfo
438{
439public:
440
441 /**
442 * Constructs a new instance that will fetch the current error info if
443 * @a aIsNull is @c false (by default) or remain uninitialized (null)
444 * otherwise.
445 *
446 * @param aIsNull @c true to prevent fetching error info and leave
447 * the instance uninitialized.
448 */
449 ErrorInfoKeeper(bool aIsNull = false)
450 : ErrorInfo(false), mForgot(aIsNull)
451 {
452 if (!aIsNull)
453 init(true /* aKeepObj */);
454 }
455
456 /**
457 * Constructs a new instance from an ErrorInfo object, to inject a full
458 * error info created elsewhere.
459 *
460 * @param aInfo @c true to prevent fetching error info and leave
461 * the instance uninitialized.
462 */
463 ErrorInfoKeeper(const ErrorInfo &aInfo)
464 : ErrorInfo(false), mForgot(false)
465 {
466 copyFrom(aInfo);
467 }
468
469 /**
470 * Destroys this instance and automatically calls #restore() which will
471 * either restore error info fetched by the constructor or do nothing
472 * if #forget() was called before destruction.
473 */
474 ~ErrorInfoKeeper() { if (!mForgot) restore(); }
475
476 /**
477 * Tries to (re-)fetch error info set on the current thread. On success,
478 * the previous error information, if any, will be overwritten with the
479 * new error information. On failure, or if there is no error information
480 * available, this instance will be reset to null.
481 */
482 void fetch()
483 {
484 setNull();
485 mForgot = false;
486 init(true /* aKeepObj */);
487 }
488
489 /**
490 * Restores error info fetched by the constructor and forgets it
491 * afterwards. Does nothing if the error info was forgotten by #forget().
492 *
493 * @return COM result of the restore operation.
494 */
495 HRESULT restore();
496
497 /**
498 * Forgets error info fetched by the constructor to prevent it from
499 * being restored by #restore() or by the destructor.
500 */
501 void forget() { mForgot = true; }
502
503 /**
504 * Forgets error info fetched by the constructor to prevent it from
505 * being restored by #restore() or by the destructor, and returns the
506 * stored error info object to the caller.
507 */
508 ComPtr<IUnknown> takeError() { mForgot = true; return mErrorInfo; }
509
510private:
511
512 bool mForgot : 1;
513};
514
515} /* namespace com */
516
517#endif
518
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette