aboutsummaryrefslogtreecommitdiff
path: root/c/win.cpp
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-08-22 03:17:54 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-08-22 03:21:52 +0200
commitd7bb04a09c7f04b0c3a8580d6a9ccd6313656fcf (patch)
tree46f91cf658a39015d821d19935b6f99867ddbfed /c/win.cpp
parentba0750ff48bcf97ef99602fe27322fa706a93b6b (diff)
downloadEpisodeBrowser-d7bb04a09c7f04b0c3a8580d6a9ccd6313656fcf.tar.gz
Add InternetError exception.
Diffstat (limited to 'c/win.cpp')
-rw-r--r--c/win.cpp86
1 files changed, 65 insertions, 21 deletions
diff --git a/c/win.cpp b/c/win.cpp
index c7ce7ec..601f82c 100644
--- a/c/win.cpp
+++ b/c/win.cpp
@@ -2,6 +2,7 @@
#include <string>
#include <SWI-Prolog.h>
#include <windows.h>
+#include <wininet.h>
#include "pl.h"
#include "util.h"
@@ -84,24 +85,29 @@ int EBMessageBox(const std::wstring_view text, const std::wstring_view caption,
void ShowException(const wchar_t* const fmt, const wchar_t* const title, const UINT uType) noexcept
{
- const wchar_t* what = L"an exception";
- WcharPtr why;
-
try {
std::rethrow_exception(std::current_exception());
} catch (const term_t& t) {
- what = L"a Prolog exception";
- try { why = PlString(t); } catch (...) {}
- } catch (const Win32Error& e) {
- what = L"a Windows error";
- try { why = WcharPtr::Copy(e.What()); } catch (...) {}
+ WcharPtr what = PlString(t);
+ std::wstring msg(wcslen(fmt)+wcslen(what), 0);
+ Swprintf(msg, fmt, static_cast<wchar_t*>(what));
+ EBMessageBox(msg, title, uType);
+ } catch (const WideException& e) {
+ std::wstring msg(wcslen(fmt)+wcslen(e.What()), 0);
+ Swprintf(msg, fmt, e.What());
+ EBMessageBox(msg, title, uType);
} catch (const std::exception& e) {
- try { why = WcharPtr::FromNarrow(e.what()); } catch (...) {}
- } catch (...) {}
+ auto what = WcharPtr::FromNarrow(e.what());
+ std::wstring msg(wcslen(fmt)+wcslen(what), 0);
+ Swprintf(msg, fmt, static_cast<wchar_t*>(what));
+ EBMessageBox(msg, title, uType);
+ } catch (...) {
+ const wchar_t* what = L"an unknown error occurred";
+ std::wstring msg(wcslen(fmt)+wcslen(what), 0);
+ Swprintf(msg, fmt, what);
+ EBMessageBox(msg, title, uType);
+ }
- std::wstring msg(wcslen(fmt)+wcslen(what)+wcslen(why), 0);
- Swprintf(msg, fmt, what, static_cast<wchar_t*>(why));
- EBMessageBox(msg, title, uType);
}
int GetRelativeCursorPos(const HWND hWnd, POINT* const pt) noexcept
@@ -118,9 +124,6 @@ int GetRelativeCursorPos(const HWND hWnd, POINT* const pt) noexcept
return 1;
}
-Win32Error::Win32Error() noexcept
- : code(GetLastError()) {}
-
Win32Error::Win32Error(const DWORD code) noexcept
: code(code) {}
@@ -136,25 +139,66 @@ const char* Win32Error::what() const noexcept
{
if (!m_szMsg)
FormatMessageA(
- FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
- nullptr,
+ FORMAT_MESSAGE_ALLOCATE_BUFFER
+ |FORMAT_MESSAGE_FROM_SYSTEM
+ |FORMAT_MESSAGE_FROM_HMODULE
+ |FORMAT_MESSAGE_IGNORE_INSERTS,
+ GetModuleHandle(L"wininet.dll"),
code,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(char*)&m_szMsg,
0, nullptr);
- return m_szMsg;
+ return m_szMsg? m_szMsg: "An unknown error occurred";
}
const wchar_t* Win32Error::What() const noexcept
{
if (!m_wszMsg)
FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
- nullptr,
+ FORMAT_MESSAGE_ALLOCATE_BUFFER
+ |FORMAT_MESSAGE_FROM_SYSTEM
+ |FORMAT_MESSAGE_FROM_HMODULE
+ |FORMAT_MESSAGE_IGNORE_INSERTS,
+ GetModuleHandle(L"wininet.dll"),
code,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(wchar_t*)&m_wszMsg,
0, nullptr);
+ return m_wszMsg? m_wszMsg: L"An unknown error occurred";
+}
+
+InternetError::InternetError(DWORD codeSystem)
+{
+ if (codeSystem != ERROR_INTERNET_EXTENDED_ERROR)
+ throw Win32Error{codeSystem};
+
+ DWORD len, cch;
+ InternetGetLastResponseInfo(&code, nullptr, &len);
+
+ cch = len+1;
+ m_szMsg = new char[cch];
+ if (!InternetGetLastResponseInfoA(&code, m_szMsg, &cch))
+ throw Win32Error{};
+
+ cch = len+1;
+ m_wszMsg = new wchar_t[cch];
+ if (!InternetGetLastResponseInfo(&code, m_wszMsg, &len))
+ throw Win32Error{};
+}
+
+InternetError::~InternetError()
+{
+ delete m_szMsg;
+ delete m_wszMsg;
+}
+
+const char* InternetError::what() const noexcept
+{
+ return m_szMsg;
+}
+
+const wchar_t* InternetError::What() const noexcept
+{
return m_wszMsg;
}