Skip to content

Commit ffd474e

Browse files
committed
CollectionInterface: convert throws to messageboxes, to avoid crashing the plugin/app
1 parent f3402c1 commit ffd474e

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

src/Classes/CollectionInterfaceClass.cpp

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
#include "json.hpp"
44

55

6-
class wruntime_error : public std::runtime_error {
7-
public:
8-
wruntime_error(const std::wstring& msg) : runtime_error("Error!"), message(msg) {};
9-
10-
std::wstring get_message() { return message; }
11-
12-
private:
13-
std::wstring message;
14-
};
15-
166
CollectionInterface::CollectionInterface(HWND hwndNpp) {
177
_hwndNPP = hwndNpp;
188
_populateNppDirs();
@@ -68,13 +58,16 @@ std::vector<char> CollectionInterface::downloadFileInMemory(const std::wstring&
6858
{
6959
HINTERNET hInternet = InternetOpen(L"CollectionInterfacePluginForN++", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
7060
if (hInternet == NULL) {
71-
throw std::runtime_error("InternetOpen failed");
61+
std::wstring errmsg = L"Could not connect to internet when trying to download\r\n" + url;
62+
::MessageBox(_hwndNPP, errmsg.c_str(), L"Download Error", MB_ICONERROR);
63+
return std::vector<char>();
7264
}
7365

7466
HINTERNET hConnect = InternetOpenUrl(hInternet, url.c_str(), NULL, 0, INTERNET_FLAG_RELOAD, 0);
7567
if (hConnect == NULL) {
76-
std::string errmsg = "InternetOpenUrl failed: " + std::to_string(GetLastError()) + "\n";
77-
throw std::runtime_error(errmsg.c_str());
68+
std::wstring errmsg = L"Could not connect to internet when trying to download\r\n" + url;
69+
::MessageBox(_hwndNPP, errmsg.c_str(), L"Download Error", MB_ICONERROR);
70+
return std::vector<char>();
7871
}
7972

8073
std::vector<char> buffer(4096);
@@ -95,20 +88,24 @@ bool CollectionInterface::downloadFileToDisk(const std::wstring& url, const std:
9588
{
9689
HINTERNET hInternet = InternetOpen(L"CollectionInterfacePluginForN++", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
9790
if (hInternet == NULL) {
98-
throw wruntime_error(L"InternetOpen failed");
91+
std::wstring errmsg = L"Could not connect to internet when trying to download\r\n" + url;
92+
::MessageBox(_hwndNPP, errmsg.c_str(), L"Download Error", MB_ICONERROR);
93+
return false;
9994
}
10095

10196
HINTERNET hConnect = InternetOpenUrl(hInternet, url.c_str(), NULL, 0, INTERNET_FLAG_RELOAD, 0);
10297
if (hConnect == NULL) {
103-
std::string errmsg = "InternetOpenUrl failed: " + std::to_string(GetLastError()) + "\n";
104-
throw std::runtime_error(errmsg.c_str());
98+
std::wstring errmsg = L"Could not connect to internet when trying to download\r\n" + url;
99+
::MessageBox(_hwndNPP, errmsg.c_str(), L"Download Error", MB_ICONERROR);
100+
return false;
105101
}
106102

107103
DWORD dwExSize = 2 * static_cast<DWORD>(path.size());
108104
std::wstring expandedPath(dwExSize, L'\0');
109105
if (!ExpandEnvironmentStrings(path.c_str(), const_cast<LPWSTR>(expandedPath.data()), dwExSize)) {
110-
std::wstring errmsg = L"ExpandEnvirontmentStrings(" + path + L") failed: " + std::to_wstring(GetLastError()) + L"\n";
111-
throw wruntime_error(errmsg.c_str());
106+
std::wstring errmsg = L"Could not understand the path \"" + path + L"\": " + std::to_wstring(GetLastError()) + L"\n";
107+
::MessageBox(_hwndNPP, errmsg.c_str(), L"Download Error", MB_ICONERROR);
108+
return false;
112109
}
113110
_wsDeleteTrailingNulls(expandedPath);
114111

@@ -126,21 +123,20 @@ bool CollectionInterface::downloadFileToDisk(const std::wstring& url, const std:
126123
NULL
127124
);
128125

129-
std::wstring errmsg = L"CreateFile(" + expandedPath + L") failed: " + std::to_wstring(GetLastError()) + L" => " + messageBuffer + L"\n";
130-
::MessageBox(NULL, errmsg.c_str(), L"Command Error", MB_ICONERROR);
131-
126+
std::wstring errmsg = L"Could not create the file \"" + expandedPath + L"\": " + std::to_wstring(GetLastError()) + L" => " + messageBuffer + L"\n";
127+
::MessageBox(NULL, errmsg.c_str(), L"Download Error", MB_ICONERROR);
132128
LocalFree(messageBuffer);
133-
134-
throw wruntime_error(errmsg.c_str());
129+
return false;
135130
}
136131

137132
std::vector<char> buffer(4096);
138133
for (DWORD dwBytesRd = 1; dwBytesRd > 0; ) {
139134
// read a chunk from the webfile
140135
BOOL stat = InternetReadFile(hConnect, buffer.data(), static_cast<DWORD>(buffer.size()), &dwBytesRd);
141136
if (!stat) {
142-
std::wstring errmsg = L"InternetReadFile(" + url + L") failed: " + std::to_wstring(GetLastError()) + L"\n";
143-
throw wruntime_error(errmsg.c_str());
137+
std::wstring errmsg = L"Error reading from URL\"" + url + L"\": " + std::to_wstring(GetLastError()) + L"\n";
138+
::MessageBox(NULL, errmsg.c_str(), L"Download Error", MB_ICONERROR);
139+
return false;
144140
}
145141

146142
// don't need to write if no bytes were read (ie, EOF)
@@ -150,8 +146,9 @@ bool CollectionInterface::downloadFileToDisk(const std::wstring& url, const std:
150146
DWORD dwBytesWr = 0;
151147
stat = WriteFile(hFile, buffer.data(), dwBytesRd, &dwBytesWr, NULL);
152148
if (!stat) {
153-
std::wstring errmsg = L"WriteFile(" + expandedPath + L") failed: " + std::to_wstring(GetLastError()) + L"\n";
154-
throw wruntime_error(errmsg.c_str());
149+
std::wstring errmsg = L"Error writing to \"" + expandedPath + L"\": " + std::to_wstring(GetLastError()) + L"\n";
150+
::MessageBox(NULL, errmsg.c_str(), L"Download Error", MB_ICONERROR);
151+
return false;
155152
}
156153
}
157154

@@ -230,7 +227,6 @@ void CollectionInterface::getListsFromJson(void)
230227
for (const auto& item : jTheme.items()) {
231228
std::wstring ws = string2wstring(item.value().get<std::string>());
232229
vThemeFiles.push_back(ws.c_str());
233-
//!! ::MessageBoxA(NULL, item.value().get<std::string>().c_str(), "IterateThemes", MB_OK);
234230
}
235231

236232
////////////////////////////////
@@ -342,8 +338,8 @@ bool CollectionInterface::_is_dir_writable(const std::wstring& path)
342338
if (hFile == INVALID_HANDLE_VALUE) {
343339
DWORD errNum = GetLastError();
344340
if (errNum != ERROR_ACCESS_DENIED) {
345-
std::wstring errmsg = L"_is_dir_writable::CreateFile[tmp](" + tmpFileName + L") failed: " + std::to_wstring(GetLastError()) + L"\n";
346-
throw wruntime_error(errmsg.c_str());
341+
std::wstring errmsg = L"Error when testing if \"" + path + L"\" is writeable: " + std::to_wstring(GetLastError()) + L"\n";
342+
::MessageBox(NULL, errmsg.c_str(), L"Directory error", MB_ICONERROR);
347343
}
348344
return false;
349345
}
@@ -374,7 +370,8 @@ std::wstring CollectionInterface::getWritableTempDir(void)
374370
tempDir.resize(MAX_PATH + 1);
375371
if (!ExpandEnvironmentStrings(L"%USERPROFILE%", const_cast<LPWSTR>(tempDir.data()), MAX_PATH + 1)) {
376372
std::wstring errmsg = L"getWritableTempDir::ExpandEnvirontmentStrings(%USERPROFILE%) failed: " + std::to_wstring(GetLastError()) + L"\n";
377-
throw wruntime_error(errmsg.c_str());
373+
::MessageBox(NULL, errmsg.c_str(), L"Directory Error", MB_ICONERROR);
374+
return L"";
378375
}
379376
_wsDeleteTrailingNulls(tempDir);
380377
}
@@ -389,7 +386,8 @@ std::wstring CollectionInterface::getWritableTempDir(void)
389386
// if that fails, no other ideas
390387
if (!_is_dir_writable(tempDir)) {
391388
std::wstring errmsg = L"getWritableTempDir() cannot find any writable directory; please make sure %TEMP% is defined and writable\n";
392-
throw wruntime_error(errmsg.c_str());
389+
::MessageBox(NULL, errmsg.c_str(), L"Directory Error", MB_ICONERROR);
390+
return L"";
393391
}
394392
return tempDir;
395393
}

0 commit comments

Comments
 (0)