Using the Error Object

The pipeline returns an error message to the browser when a component fails (for example, if the Scriptor component is configured with a non-existent script). When writing a component, you should provide a descriptive error message that is displayed if the component fails for any reason. Because there is only one ERROR object per thread, you should always attempt to obtain the existing object and append your error description to that object before creating a new object. If the call to GetErrorInfo fails, no ERROR object has been created and you can then call CreateErrorInfo. For example:

ICreateErrorInfo*    pCErrInfo = NULL;
IErrorInfo*            pErrInfo = NULL;
HRESULT                hr = S_OK;
BSTR                    oldError = 0;
BSTR                    newError = 0;

if (GetErrorInfo(0, &pErrInfo) == S_OK) {

    hr = pErrInfo->GetDescription(&oldError);
    if (FAILED(hr)) goto error;

    newError = appendError(szDescription, oldError);

    pErrInfo->Release();
}
else {    // Call to GetErrorInfo failed
    newError = SysAllocString(szDescription);
}

hr = CreateErrorInfo(&pCErrInfo);
if (FAILED(hr)) goto error;

hr = pCErrInfo->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &pErrInfo);
if (FAILED(hr)) goto error;

hr = pCErrInfo->SetDescription(newError);
if (FAILED(hr)) goto error;

hr = pCErrInfo->SetGUID(riidCurrentInterface);
if (FAILED(hr)) goto error;

hr = pCErrInfo->SetHelpContext(dwHelpContext);
if (FAILED(hr)) goto error;

if (szHelpFile) {
    hr = pCErrInfo->SetHelpFile((LPOLESTR)szHelpFile);
    if (FAILED(hr)) goto error;
} else {
    hr = pCErrInfo->SetHelpFile( L"" );
    if (FAILED(hr)) goto error;
}

hr = pCErrInfo->SetSource((LPOLESTR)szSource);
if (FAILED(hr)) goto error;

// Set the structure
hr = SetErrorInfo(0, pErrInfo);

error:

if(pErrInfo) pErrInfo->Release();
if(pCErrInfo) pCErrInfo->Release();

static BSTR appendError(LPCWSTR newError, LPCWSTR oldError)
{
BSTR errorList;

LPOLESTR sep = L"\r\n";
int len = wcslen((LPWSTR)newError) +
    wcslen(sep) +
    SysStringLen((LPWSTR)oldError) + 1;
errorList = SysAllocStringLen(0, len);

wcscpy(errorList, (LPWSTR)newError);
wcscat(errorList, L"\n\r");
wcscat(errorList, (LPWSTR)oldError);
return errorList;
}

Related Topics

For more information, search for the following topics in MSDN:


© 1997-1998 Microsoft Corporation. All rights reserved.