From 3a51daeace695ead38cfd82b3a0f1e6f25a32e0f Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 24 May 2018 15:47:30 +0200 Subject: Improve re-throwing of UNO exceptions (*) if we are already throwing a Wrapped*Exception, get the exception using cppu::getCaughtexception. (*) when catching and then immediately throwing UNO exceptions, use cppu::getCaughtException to prevent exception slicing (*) if we are going to catch an exception and then immediately throw a RuntimeException, rather throw a WrappedTargetRuntimeException and preserve the original exception information. Change-Id: Ia7a501a50ae0e6f4d05186333c8517fdcb17d558 Reviewed-on: https://gerrit.libreoffice.org/54692 Tested-by: Jenkins Reviewed-by: Noel Grandin --- package/source/manifest/ManifestWriter.cxx | 6 +++++- package/source/xstor/ohierarchyholder.cxx | 12 ++++++++---- package/source/zippackage/ZipPackage.cxx | 30 +++++++++++++++++------------ package/source/zippackage/zipfileaccess.cxx | 7 ++++--- 4 files changed, 35 insertions(+), 20 deletions(-) (limited to 'package') diff --git a/package/source/manifest/ManifestWriter.cxx b/package/source/manifest/ManifestWriter.cxx index d349eaadeaa6..bedb6121d37c 100644 --- a/package/source/manifest/ManifestWriter.cxx +++ b/package/source/manifest/ManifestWriter.cxx @@ -19,11 +19,13 @@ #include "ManifestWriter.hxx" #include "ManifestExport.hxx" +#include #include #include #include #include #include +#include #include #include #include @@ -63,7 +65,9 @@ void SAL_CALL ManifestWriter::writeManifestSequence( const Reference< XOutputStr } catch( SAXException& ) { - throw RuntimeException( THROW_WHERE ); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( THROW_WHERE, + nullptr, anyEx ); } } diff --git a/package/source/xstor/ohierarchyholder.cxx b/package/source/xstor/ohierarchyholder.cxx index c07c7740c502..508107ac7c53 100644 --- a/package/source/xstor/ohierarchyholder.cxx +++ b/package/source/xstor/ohierarchyholder.cxx @@ -27,6 +27,7 @@ #include #include #include +#include #include "ohierarchyholder.hxx" @@ -275,9 +276,11 @@ void SAL_CALL OHierarchyElement_Impl::disposing( const lang::EventObject& Source TestForClosing(); } - catch( uno::Exception& ) + catch( uno::Exception& ex ) { - throw uno::RuntimeException(); // no exception must happen here, usually an exception means disaster + css::uno::Any anyEx = cppu::getCaughtException(); + throw lang::WrappedTargetRuntimeException( ex.Message, + nullptr, anyEx ); // no exception must happen here, usually an exception means disaster } } @@ -309,12 +312,13 @@ void SAL_CALL OHierarchyElement_Impl::commited( const css::lang::EventObject& /* { Commit(); } - catch( const uno::Exception& e ) + catch( const uno::Exception& ) { + css::uno::Any anyEx = cppu::getCaughtException(); throw lang::WrappedTargetRuntimeException( "Can not commit storage sequence!", uno::Reference< uno::XInterface >(), - uno::makeAny( e ) ); + anyEx ); } } diff --git a/package/source/zippackage/ZipPackage.cxx b/package/source/zippackage/ZipPackage.cxx index a04580dcaeae..45eda6d4837e 100644 --- a/package/source/zippackage/ZipPackage.cxx +++ b/package/source/zippackage/ZipPackage.cxx @@ -1037,12 +1037,13 @@ void ZipPackage::WriteMimetypeMagicFile( ZipOutputStream& aZipOut ) aZipOut.rawWrite(aType); aZipOut.rawCloseEntry(); } - catch ( const css::io::IOException & r ) + catch ( const css::io::IOException & ) { + css::uno::Any anyEx = cppu::getCaughtException(); throw WrappedTargetException( THROW_WHERE "Error adding mimetype to the ZipOutputStream!", static_cast < OWeakObject * > ( this ), - makeAny( r ) ); + anyEx ); } } @@ -1432,10 +1433,11 @@ void SAL_CALL ZipPackage::commitChanges() { xTempInStream = writeTempFile(); } - catch (const ucb::ContentCreationException& r) + catch (const ucb::ContentCreationException&) { + css::uno::Any anyEx = cppu::getCaughtException(); throw WrappedTargetException(THROW_WHERE "Temporary file should be creatable!", - static_cast < OWeakObject * > ( this ), makeAny ( r ) ); + static_cast < OWeakObject * > ( this ), anyEx ); } if ( xTempInStream.is() ) { @@ -1445,10 +1447,11 @@ void SAL_CALL ZipPackage::commitChanges() { xTempSeek->seek( 0 ); } - catch( const uno::Exception& r ) + catch( const uno::Exception& ) { + css::uno::Any anyEx = cppu::getCaughtException(); throw WrappedTargetException(THROW_WHERE "Temporary file should be seekable!", - static_cast < OWeakObject * > ( this ), makeAny ( r ) ); + static_cast < OWeakObject * > ( this ), anyEx ); } try @@ -1456,10 +1459,11 @@ void SAL_CALL ZipPackage::commitChanges() // connect to the temporary stream ConnectTo( xTempInStream ); } - catch( const io::IOException& r ) + catch( const io::IOException& ) { + css::uno::Any anyEx = cppu::getCaughtException(); throw WrappedTargetException(THROW_WHERE "Temporary file should be connectable!", - static_cast < OWeakObject * > ( this ), makeAny ( r ) ); + static_cast < OWeakObject * > ( this ), anyEx ); } if ( m_eMode == e_IMode_XStream ) @@ -1484,10 +1488,11 @@ void SAL_CALL ZipPackage::commitChanges() // after successful truncation the original file contents are already lost xTruncate->truncate(); } - catch( const uno::Exception& r ) + catch( const uno::Exception& ) { + css::uno::Any anyEx = cppu::getCaughtException(); throw WrappedTargetException(THROW_WHERE "This package is read only!", - static_cast < OWeakObject * > ( this ), makeAny ( r ) ); + static_cast < OWeakObject * > ( this ), anyEx ); } try @@ -1579,15 +1584,16 @@ void SAL_CALL ZipPackage::commitChanges() // if the file is still not corrupted, it can become after the next step aContent.executeCommand ("transfer", Any(aInfo) ); } - catch ( const css::uno::Exception& r ) + catch ( const css::uno::Exception& ) { if ( bCanBeCorrupted ) DisconnectFromTargetAndThrowException_Impl( xTempInStream ); + css::uno::Any anyEx = cppu::getCaughtException(); throw WrappedTargetException( THROW_WHERE "This package may be read only!", static_cast < OWeakObject * > ( this ), - makeAny ( r ) ); + anyEx ); } } } diff --git a/package/source/zippackage/zipfileaccess.cxx b/package/source/zippackage/zipfileaccess.cxx index eb5ad53f1ce7..f2b1015a7055 100644 --- a/package/source/zippackage/zipfileaccess.cxx +++ b/package/source/zippackage/zipfileaccess.cxx @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -282,11 +283,11 @@ uno::Any SAL_CALL OZipFileAccess::getByName( const OUString& aName ) { throw; } - catch (const uno::Exception& e) + catch (const uno::Exception&) { + css::uno::Any anyEx = cppu::getCaughtException(); throw lang::WrappedTargetException( "This package is unusable!", - static_cast < OWeakObject * > ( this ), - makeAny(e)); + static_cast < OWeakObject * > ( this ), anyEx); } if ( !xEntryStream.is() ) -- cgit v1.2.3