From 6ae7e24675cff4ff6b808c3024f45083f35ced97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 3 Sep 2022 15:28:56 +0200 Subject: Improve error handling. --- c/win32.cpp | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) (limited to 'c/win32.cpp') diff --git a/c/win32.cpp b/c/win32.cpp index 2686f2e..2ecea50 100644 --- a/c/win32.cpp +++ b/c/win32.cpp @@ -4,6 +4,7 @@ #include #include +#include "err.h" #include "util.h" #include "win32.h" #include "window.h" @@ -16,7 +17,7 @@ std::wstring WideFromNarrow(const std::string_view src, const int cp) int cchWide = MultiByteToWideChar(cp, 0, src.data(), cchNarrow, nullptr, 0); std::wstring dst(cchWide, 0); if (!MultiByteToWideChar(cp, 0, src.data(), cchNarrow, dst.data(), cchWide)) - throw Win32Error(); + throw Err(WINDOWS, L"Narrow string could not be converted to wide string: %s"); return dst; } @@ -95,34 +96,6 @@ int EBMessageBox(const std::wstring_view text, const std::wstring_view caption, return MessageBox(hWnd, text.data(), caption.data(), uType); } -void Act(const wchar_t* action) -{ - s_action = action; -} - -void ShowException(const wchar_t* const fmt, const wchar_t* const title, const UINT uType) noexcept -{ - if (!s_action) - s_action = L"performing an unknown action"; - try { - std::rethrow_exception(std::current_exception()); - } catch (const WideException& e) { - std::wstring msg(wcslen(fmt)+wcslen(s_action)+wcslen(e.What()), 0); - Swprintf(msg, fmt, s_action, e.What()); - EBMessageBox(msg, title, uType); - } catch (const std::exception& e) { - std::wstring what = WideFromNarrow(e.what()); - std::wstring msg(wcslen(fmt)+wcslen(s_action)+what.length(), 0); - Swprintf(msg, fmt, s_action, what.c_str()); - EBMessageBox(msg, title, uType); - } catch (...) { - const wchar_t* what = L"an unknown error occurred"; - std::wstring msg(wcslen(fmt)+wcslen(s_action)+wcslen(what), 0); - Swprintf(msg, fmt, s_action, what); - EBMessageBox(msg, title, uType); - } -} - int GetRelativeCursorPos(const HWND hWnd, POINT* const pt) noexcept { RECT rc; @@ -229,7 +202,7 @@ Library::Library(const wchar_t* const lib) { m_hModule = LoadLibraryW(lib); if (!m_hModule) - throw Win32Error(); + throw Err(WINDOWS, L"Library "s + lib + L" could not be loaded: %s"); } Library::~Library() -- cgit v1.2.3