From 28b87e4d9c60b49d46b03a19b7b83e23e222087a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 22 Aug 2022 01:02:34 +0200 Subject: Handle exceptions in fetching thread. --- c/data.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'c/data.cpp') diff --git a/c/data.cpp b/c/data.cpp index 4bded44..771e6fb 100644 --- a/c/data.cpp +++ b/c/data.cpp @@ -70,7 +70,7 @@ static inline void XmlFree(void* p) { xmlFree(p); } using XmlCharPtr = XmlPtr; template -bool WcharsFromXmlchars(wchar_t (&dst)[N], XmlCharPtr utf8) +bool WcharsFromXmlchars(wchar_t (&dst)[N], XmlCharPtr utf8) noexcept { /* Truncate if source is larger than destination. */ int lenUtf8 = xmlStrlen(utf8); @@ -94,7 +94,7 @@ bool WcharsFromXmlchars(wchar_t (&dst)[N], XmlCharPtr utf8) return MultiByteToWideChar(CP_UTF8, 0, src, cchNarrow, dst, cchWide); } -void FetchData(bool* bDone) +void FetchData() { LIBXML_TEST_VERSION; @@ -123,7 +123,9 @@ void FetchData(bool* bDone) xmlNodeSetPtr nodes = xpathObj->nodesetval; int cNodes = nodes? nodes->nodeNr: 0; - printf("%d nodes\n", cNodes); + if (!cNodes) + throw std::runtime_error("could not find remote episode information"); + for (int i = 0; i < cNodes; i++) { extern FileView g_fvElv; extern FileView g_fvDlv; @@ -153,8 +155,18 @@ void FetchData(bool* bDone) /* Get wiki URL. */ const xmlNodePtr nodeLink = xmlFirstElementChild(nodeTitle); if (nodeLink) - WcharsFromXmlchars(d.wiki, xmlGetProp(nodeLink, (const xmlChar*)"href")); + WcharsFromXmlchars(d.wiki, + xmlGetProp(nodeLink, reinterpret_cast("href"))); } +} - *bDone = true; +void WaitFetchData(bool* bDone) noexcept +{ + try { + FetchData(); + *bDone = true; + } catch (...) { + *bDone = true; + ShowException(L"Remote data could not be fetched due to %s: %s", L"Error", MB_ICONWARNING); + } } -- cgit v1.2.3