diff options
author | John Ankarström <john@ankarstrom.se> | 2022-09-03 15:28:56 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-09-03 15:28:56 +0200 |
commit | 6ae7e24675cff4ff6b808c3024f45083f35ced97 (patch) | |
tree | 2a47273f325d8367db2a8669691273f02ca83eb9 /c/err.cpp | |
parent | 2cd22c671c67deaf2c1fcb659e3262bf57552557 (diff) | |
download | EpisodeBrowser-6ae7e24675cff4ff6b808c3024f45083f35ced97.tar.gz |
Improve error handling.
Diffstat (limited to 'c/err.cpp')
-rw-r--r-- | c/err.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/c/err.cpp b/c/err.cpp new file mode 100644 index 0000000..fd35ddd --- /dev/null +++ b/c/err.cpp @@ -0,0 +1,87 @@ +#include <exception> +#include <windows.h> +#include <wininet.h> +#include <libxml/xmlerror.h> + +#include "err.h" +#include "util.h" +#include "win32.h" + +Err::Err(ErrType t, const wchar_t* fmt) +{ + switch (t) { + case GENERIC: + what = fmt; + break; + case WINDOWS: + { + wchar_t* msg; + DWORD lenMsg = FormatMessageW( + FORMAT_MESSAGE_ALLOCATE_BUFFER + |FORMAT_MESSAGE_FROM_SYSTEM + |FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, + GetLastError(), + MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), + (wchar_t*)&msg, + 0, nullptr); + what = std::wstring(lenMsg+wcslen(fmt), 0); + Swprintf(what, fmt, msg); + HeapFree(GetProcessHeap(), 0, msg); + break; + } + case WININET: + { + DWORD code = GetLastError(); + if (code == ERROR_INTERNET_EXTENDED_ERROR) { + DWORD lenMsg; + InternetGetLastResponseInfo(&code, nullptr, &lenMsg); + std::wstring msg(lenMsg, 0); + if (InternetGetLastResponseInfoW(&code, msg.data(), &lenMsg)) { + what = std::wstring(lenMsg+wcslen(fmt), 0); + Swprintf(what, fmt, msg.c_str()); + EBMessageBox(msg, L"Test"); + } else + what = Err(WINDOWS, fmt).what; + } else { + wchar_t* wszMsg; + DWORD lenMsg = FormatMessageW( + 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*)&wszMsg, + 0, nullptr); + what = std::wstring(lenMsg+wcslen(fmt), 0); + Swprintf(what, fmt, wszMsg); + HeapFree(GetProcessHeap(), 0, wszMsg); + } + break; + } + case LIBXML2: + { + std::wstring msg = WideFromNarrow(xmlGetLastError()->message); + what = std::wstring(msg.size()+wcslen(fmt), 0); + Swprintf(what, fmt, msg.c_str()); + break; + } + } +} + +std::wstring What(std::exception_ptr ex) +{ + try { + std::rethrow_exception(ex); + } catch (const Err& e) { + return e.what; + } catch (const WideException& e) { + return e.What(); + } catch (const std::exception& e) { + return WideFromNarrow(e.what()); + } catch (...) { + return L"Unknown exception"; + } +} |