/* $Revision: 17 $ $Date: 18/02/06 12:32 $ Copyright © 2002-2003, FSL Technologies Limited. Contact "http://fost3.fsltech.com". */ #include "stdafx.h" #include using namespace std; using namespace FSLib; using namespace FSLib::Exceptions; namespace { Revision c_revision( L"$Archive: /FOST.3/f3util/comException.cpp $", __DATE__, L"$Revision: 17 $", L"$Date: 18/02/06 12:32 $" ); const wchar_t ComErrorMsg[]=L"COM Error"; const wchar_t IISErrorMsg[]=L"ASP/IIS specific error"; const wchar_t PageStartEndMsg[]=L"An error in the page handling has occured"; const wchar_t FieldMsg[]=L"Problem fetching field from data source"; } /* FSLib::Exceptions::ComError */ FSLib::Exceptions::ComError::ComError( const FSLib::wstring &message ) : Exception( message ) { } FSLib::Exceptions::ComError::ComError( const FSLib::wstring &message, const FSLib::wstring &information ) : Exception( message ) { info() << trim( information ) << std::endl; } FSLib::Exceptions::ComError::ComError( const _com_error &c ) : Exception() { m_info << L"Details:" << endl; m_info << L" Description: "; if ( !c.Description() ) { m_info << L"Unknown COM error - No error message contained in the exception decription." << endl; } else { m_info << L"\'" << BSTR_to_wstring( c.Description() ) << L"\'" << endl; } m_info << L" Source: "; if ( !c.Source() ) { m_info << L"Unknown source - No source contained in the exception description." << endl; } else { m_info << L"\'"<< BSTR_to_wstring( c.Source() ) << L"\'" << endl; } m_info << L" Error Message: "; if ( !c.ErrorMessage() ) { m_info << L"Unknown" << endl; } else { m_info << L"\'" << c.ErrorMessage() << L"\'" << endl; } } FSLib::Exceptions::ComError::ComError( const _com_error &c, const FSLib::wstring &s ) : Exception() { m_info << s << endl; m_info << L"Details:" << endl; m_info << L" Description: "; if ( !c.Description() ) { m_info << L"Unknown COM error - No error message contained in the exception decription." << endl; } else { m_info << L"\'" << BSTR_to_wstring( c.Description() ) << L"\'" << endl; } m_info << L" Source: "; if ( !c.Source() ) { m_info << L"Unknown" << endl; } else { m_info << L"\'" << BSTR_to_wstring( c.Source() ) << L"\'" << endl; } m_info << L" Error Message: "; if ( !c.ErrorMessage() ) { m_info << L"Unknown" << endl; } else { m_info << L"\'" << c.ErrorMessage() << L"\'" << endl; } } inline const wchar_t * const FSLib::Exceptions::ComError::message() const { return ComErrorMsg; } /* FSLib::HTML::Exceptions::IISError */ inline FSLib::Exceptions::IISError::IISError( const FSLib::wstring &error ) : FSLib::Exceptions::Exception( error ) { } inline FSLib::Exceptions::IISError::IISError( const FSLib::wstring &error, const FSLib::wstring &diag ) : FSLib::Exceptions::Exception( error ) { m_info << diag << std::endl; } inline const wchar_t * const FSLib::Exceptions::IISError::message() const { return IISErrorMsg; } /* FSLib::Exceptions::PageStartEnd */ FSLib::Exceptions::PageStartEnd::PageStartEnd(const FSLib::wstring &m) : IISError( m ) { } FSLib::Exceptions::PageStartEnd::PageStartEnd( const FSLib::wstring &m, const FSLib::wstring &aux ) : IISError( m ) { info() << aux << endl; } inline const wchar_t * const FSLib::Exceptions::PageStartEnd::message() const { return PageStartEndMsg; } /* FSLib::Exceptions::Field */ inline FSLib::Exceptions::Field::Field( const FSLib::wstring &msg ) : Exception( msg ) { } inline FSLib::Exceptions::Field::Field( const FSLib::wstring &msg, const FSLib::wstring &name ) : Exception( msg ) { m_info << L"Field name: '" << name << L"'" << endl; } inline const wchar_t * const FSLib::Exceptions::Field::message() const { return FieldMsg; } /* FSLib::ComHR */ FSLib::ComHR HR; inline FSLib::ComHR::ComHR( HRESULT hr, size_t line, const char *file ) { try { check( hr ); } catch ( FSLib::Exceptions::Exception &e ) { e.info() << widen( file ) << L":" << toString( line ) << std::endl; throw; } } void FSLib::ComHR::doThrow( HRESULT hr ) const { throw FSLib::Exceptions::ComError( L"COM call failed - bad HRESULT (" + format( hr ) + L")" ); } FSLib::wstring FSLib::ComHR::format( HRESULT hr ) { switch ( hr ) { case CLASS_E_NOAGGREGATION: return L"CLASS_E_NOAGGREGATION"; case E_NOINTERFACE: return L"E_NOINTERFACE"; case REGDB_E_CLASSNOTREG: return L"REGDB_E_CLASSNOTREG"; default: return toString( hr ); } } FSLib::wstring FSLib::ComHR::format( IUnknown * punk ) { CComBSTR bstrError; ATL::CComPtr pSEI; HRESULT hr = punk->QueryInterface(IID_ISupportErrorInfo,(void **) &pSEI); if(SUCCEEDED(hr)){ ATL::CComPtr pEO; if(S_OK == GetErrorInfo(NULL, &pEO)){ CComBSTR bstrDesc; pEO->GetDescription(&bstrDesc); return BSTR_to_wstring( bstrDesc ); } } return L"Could not find error information on supplied interface"; }