Skip to content

Commit

Permalink
Rephrase WinHttp-based exceptions to be more humane
Browse files Browse the repository at this point in the history
  • Loading branch information
vslavik committed Apr 21, 2024
1 parent f593952 commit 404cb6a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
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
61 changes: 51 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,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");
}
}

0 comments on commit 404cb6a

Please sign in to comment.