Skip to content

Commit

Permalink
Rephrase WinHttp-based exceptions to be more humane
Browse files Browse the repository at this point in the history
Increase required C++REST SDK version to 2.10.8, because it fixes a bug that affects code
in this commit:
microsoft/cpprestsdk@aabec3c
  • Loading branch information
vslavik committed Apr 25, 2024
1 parent a5059f3 commit 52ee0cc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
6 changes: 3 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ AS_IF([test "x$with_cpprest" != "xno"],
CPPFLAGS="$CPPFLAGS $CXXFLAGS $BOOST_CPPFLAGS"
AC_CHECK_HEADERS([cpprest/http_client.h],
[
AC_MSG_CHECKING([for libcpprest >= 2.5])
AC_MSG_CHECKING([for libcpprest >= 2.10.8])
old_LIBS="$LIBS"
LIBS="-lcpprest $BOOST_SYSTEM_LIB $BOOST_THREAD_LIB -lssl -lcrypto $LIBS"
AC_LINK_IFELSE([AC_LANG_PROGRAM(
Expand All @@ -88,8 +88,8 @@ AS_IF([test "x$with_cpprest" != "xno"],
#include <cpprest/http_client.h>
],
[
#if CPPREST_VERSION < 200500
#error "cpprest >= 2.5 required"
#if CPPREST_VERSION < 201008
#error "cpprest >= 2.10.8 required"
#endif
web::http::client::http_client c(U("https://poedit.net"));
])],
Expand Down
2 changes: 1 addition & 1 deletion src/Poedit-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "concurrency.h"
#include "configuration.h"

#include "errors.h"
#include "unicode_helpers.h"
#include "str_helpers.h"
#endif
Expand Down
59 changes: 49 additions & 10 deletions src/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@

#include "errors.h"

#include <wx/translation.h>

#ifndef __WXOSX__
#include <cpprest/http_client.h>
#include <boost/algorithm/string.hpp>
#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
Expand All @@ -36,21 +64,32 @@ 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;

msg = e.error_code().message();
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");
}
}

0 comments on commit 52ee0cc

Please sign in to comment.