From 281395aaaf8e506dada1b338ec166998afa22a2f Mon Sep 17 00:00:00 2001 From: VinsWorldcom Date: Fri, 18 Feb 2022 09:49:44 -0500 Subject: [PATCH] nicer error messages on Tortoise/Git not found --- PluginDefinition.cpp | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/PluginDefinition.cpp b/PluginDefinition.cpp index aab3bf9..b34df9d 100644 --- a/PluginDefinition.cpp +++ b/PluginDefinition.cpp @@ -241,6 +241,28 @@ std::wstring getCurrentFileDirectory() return std::wstring(path); } +std::wstring GetLastErrorString(DWORD errorCode) +{ + std::wstring errorMsg(_T("")); + // Get the error message, if any. + // If both error codes (passed error n GetLastError) are 0, then return empty + if (errorCode == 0) + errorCode = GetLastError(); + if (errorCode == 0) + return errorMsg; //No error message has been recorded + + LPWSTR messageBuffer = nullptr; + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr); + + errorMsg += messageBuffer; + + //Free the buffer. + LocalFree(messageBuffer); + + return errorMsg; +} + /// /// Gets the path to the TortioseGit executable from the registry. /// @@ -353,8 +375,12 @@ void ExecGitCommand( command += TEXT( "\"" ); if ( !launchGit( command ) ) - MessageBox( nppData._nppHandle, TEXT( "Could not launch Git." ), + { + std::wstring err = TEXT( "Could not launch Git.\n\n" ); + err += GetLastErrorString(GetLastError()); + MessageBox( nppData._nppHandle, err.c_str(), TEXT( "Failed" ), ( MB_OK | MB_ICONWARNING | MB_APPLMODAL ) ); + } updatePanel(); } @@ -396,8 +422,12 @@ void ExecTortoiseCommand( command += TEXT( "\" /closeonend:2" ); if ( !launchGit( command ) ) - MessageBox( nppData._nppHandle, TEXT( "Could not launch TortoiseGit." ), + { + std::wstring err = TEXT( "Could not launch TortoiseGit.\n\n" ); + err += GetLastErrorString(GetLastError()); + MessageBox( nppData._nppHandle, err.c_str(), TEXT( "Failed" ), ( MB_OK | MB_ICONWARNING | MB_APPLMODAL ) ); + } updatePanel(); }