From 404cb6a5d6704c72d899811f2a5b90b9d5b11b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Thu, 18 Apr 2024 15:48:44 +0200 Subject: [PATCH] Rephrase WinHttp-based exceptions to be more humane --- src/Poedit-Prefix.pch | 2 +- src/errors.cpp | 61 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/Poedit-Prefix.pch b/src/Poedit-Prefix.pch index 7079c9557b..40adbbeeb1 100644 --- a/src/Poedit-Prefix.pch +++ b/src/Poedit-Prefix.pch @@ -12,7 +12,7 @@ #include "concurrency.h" #include "configuration.h" - + #include "errors.h" #include "unicode_helpers.h" #include "str_helpers.h" #endif diff --git a/src/errors.cpp b/src/errors.cpp index 7e3198ed87..c6918ba034 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -25,6 +25,34 @@ #include "errors.h" +#include + +#ifndef __WXOSX__ +#include +#include +#endif + +namespace +{ + +inline wxString from_c_string(const char *msg) +{ + // try interpreting as UTF-8 first as the most likely one (from external sources) + wxString s = wxString::FromUTF8(msg); + if (!s.empty()) + return s; + + s = wxString(msg); + if (!s.empty()) + return s; + + // not in current locale either, fall back to Latin1 + return wxString(msg, wxConvISO8859_1); +} + +} // anonymous namespace + + wxString errors::detail::DescribeExceptionImpl(Rethrower& rethrower) { try @@ -36,21 +64,34 @@ wxString errors::detail::DescribeExceptionImpl(Rethrower& rethrower) { return e.What(); } - catch (const std::exception& e) +#ifndef __WXOSX__ + catch (const web::http::http_exception & e) { - const char *msg = e.what(); - // try interpreting as UTF-8 first as the most likely one (from external sources) - wxString s = wxString::FromUTF8(msg); - if (s.empty()) + // rephrase the errors more humanly; the default form is too cryptic + // also strip trailing newlines that C++REST tends to add + std::string msg(e.what()); + if (!boost::starts_with(msg, "WinHttp")) { - s = wxString(msg); - if (s.empty()) // not in current locale either, fall back to Latin1 - s = wxString(msg, wxConvISO8859_1); + boost::trim_right(msg); + return from_c_string(msg.c_str()); // preserve actual messages } - return s; + + // ".c_str()" here is a workaround for bug in older versions of C++REST SDK: + // https://github.com/microsoft/cpprestsdk/commit/aabec3cbca699f238e9b78226c834b9789fdbbb6 + msg = e.error_code().message().c_str(); + if (msg.empty()) + return from_c_string(e.what()); // give up + + boost::trim_right(msg); + return wxString::Format(_("Network error: %s (%d)"), from_c_string(msg.c_str()), e.error_code().value()); + } +#endif // !__WXOSX__ + catch (const std::exception& e) + { + return from_c_string(e.what()); } catch (...) { - return "unknown error"; + return _("Unknown error"); } }