Skip to content

Commit

Permalink
Fix error reporting in MSVC 2015
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Dec 1, 2024
1 parent c12dff7 commit b3fdbe2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/bit7zlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ using namespace bit7z;

Bit7zLibrary::Bit7zLibrary( const tstring& libraryPath ) : mLibrary( Bit7zLoadLibrary( libraryPath ) ) {
if ( mLibrary == nullptr ) {
throw BitException( "Failed to load the 7-zip library", ERROR_CODE( std::errc::bad_file_descriptor ) );
const auto error = ERROR_CODE( std::errc::bad_file_descriptor );
throw BitException( "Failed to load the 7-zip library", error );
}

mCreateObjectFunc = GetProcAddress( mLibrary, "CreateObject" );

if ( mCreateObjectFunc == nullptr ) {
FreeLibrary( mLibrary );
throw BitException( "Failed to get CreateObject function", ERROR_CODE( std::errc::invalid_seek ) );
const auto error = ERROR_CODE( std::errc::invalid_seek );
throw BitException( "Failed to get CreateObject function", error );
}
}

Expand All @@ -56,7 +58,8 @@ void Bit7zLibrary::setLargePageMode() {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto pSetLargePageMode = reinterpret_cast< SetLargePageMode >( GetProcAddress( mLibrary, "SetLargePageMode" ) );
if ( pSetLargePageMode == nullptr ) {
throw BitException( "Failed to get SetLargePageMode function", ERROR_CODE( std::errc::invalid_seek ) );
const auto error = ERROR_CODE( std::errc::invalid_seek );
throw BitException( "Failed to get SetLargePageMode function", error );
}
const HRESULT res = pSetLargePageMode();
if ( res != S_OK ) {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/cfileinstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ void CFileInStream::openFile( const fs::path& filePath ) {
if ( mFileStream.fail() ) {
#if defined( __MINGW32__ ) || defined( __MINGW64__ )
std::error_code error{ errno, std::generic_category() };
throw BitException( "Failed to open the archive file", error, path_to_tstring( filePath ) );
#else
throw BitException( "Failed to open the archive file", last_error_code(), path_to_tstring( filePath ) );
const auto error = last_error_code();
#endif
throw BitException( "Failed to open the archive file", error, path_to_tstring( filePath ) );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/internal/cfileoutstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ CFileOutStream::CFileOutStream( fs::path filePath, bool createAlways )
if ( mFileStream.fail() ) {
#if defined( __MINGW32__ ) || defined( __MINGW64__ )
error = std::error_code{ errno, std::generic_category() };
throw BitException( "Failed to open the output file", error, path_to_tstring( mFilePath ) );
#else
throw BitException( "Failed to open the output file", last_error_code(), path_to_tstring( mFilePath ) );
error = last_error_code();
#endif
throw BitException( "Failed to open the output file", error, path_to_tstring( mFilePath ) );
}
// Unbuffered streams are slow for Visual Studio 2015
#if !defined(_MSC_VER) || _MSC_VER != 1900
Expand Down
3 changes: 2 additions & 1 deletion src/internal/fsitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ FilesystemItem::FilesystemItem( fs::directory_entry entry, const fs::path& searc
void FilesystemItem::initAttributes( const fs::path& itemPath ) {
if ( !fsutil::get_file_attributes_ex( itemPath.c_str(), mSymlinkPolicy, mFileAttributeData ) ) {
//should not happen, but anyway...
throw BitException( "Could not retrieve file attributes", last_error_code(), path_to_tstring( itemPath ) );
const auto error = last_error_code();
throw BitException( "Could not retrieve file attributes", error, path_to_tstring( itemPath ) );
}
}

Expand Down

0 comments on commit b3fdbe2

Please sign in to comment.