diff --git a/src/tl/tl/tlHttpStream.cc b/src/tl/tl/tlHttpStream.cc index e6900a432..04e18f74d 100644 --- a/src/tl/tl/tlHttpStream.cc +++ b/src/tl/tl/tlHttpStream.cc @@ -26,6 +26,30 @@ namespace tl { - // .. nothing yet .. +// --------------------------------------------------------------- +// HttpErrorException implementation + +std::string +HttpErrorException::format_error (const std::string &em, int ec, const std::string &url, const std::string &body) +{ + std::string msg = tl::sprintf (tl::to_string (tr ("Error %d: %s, fetching %s")), ec, em, url); + + if (! body.empty ()) { + + msg += "\n\n"; + msg += tl::to_string (tr ("Reply body:")); + msg += "\n"; + + if (body.size () > 1000) { + msg += std::string (body.c_str (), 1000); + msg += "..."; + } else { + msg += body; + } + + } + + return msg; +} } diff --git a/src/tl/tl/tlHttpStream.h b/src/tl/tl/tlHttpStream.h index cd2884657..30a8f8339 100644 --- a/src/tl/tl/tlHttpStream.h +++ b/src/tl/tl/tlHttpStream.h @@ -56,11 +56,11 @@ class TL_PUBLIC HttpErrorException : public tl::Exception { public: - HttpErrorException (const std::string &f, int ec, QNetworkReply *reply) - : tl::Exception (format_error (f, ec, reply)) + HttpErrorException (const std::string &f, int ec, const std::string &url, const std::string &body = std::string ()) + : tl::Exception (format_error (f, ec, url, body)) { } - static std::string format_error (const std::string &em, int ec, QNetworkReply *reply); + static std::string format_error (const std::string &em, int ec, const std::string &url, const std::string &body); }; class InputHttpStreamPrivateData; diff --git a/src/tl/tl/tlHttpStreamQt.cc b/src/tl/tl/tlHttpStreamQt.cc index e53b94f57..f31426767 100644 --- a/src/tl/tl/tlHttpStreamQt.cc +++ b/src/tl/tl/tlHttpStreamQt.cc @@ -123,39 +123,6 @@ AuthenticationHandler::proxyAuthenticationRequired (const QNetworkProxy &proxy, // --------------------------------------------------------------- // InputHttpStream implementation -std::string -HttpErrorException::format_error (const std::string &em, int ec, QNetworkReply *reply) -{ - std::string msg = tl::sprintf (tl::to_string (tr ("Error %d: %s, fetching %s")), ec, em, tl::to_string (reply->url ().toString ())); - - std::string data = tl::to_string (QString::fromUtf8 (reply->readAll ())); - if (data.size () > 1000) { - data = data.substr (0, size_t (1000)) + "..."; - } - - if (! data.empty ()) { - - msg += "\n\n"; - msg += tl::to_string (tr ("Reply body:")); - msg += "\n"; - msg += data; - - } - - std::string redirected = tl::to_string (reply->attribute (QNetworkRequest::RedirectionTargetAttribute).toString ()); - if (! redirected.empty ()) { - - msg += "\n\n"; - msg += tl::to_string (tr ("Redirected to: ")) + redirected; - - } - - return msg; -} - -// --------------------------------------------------------------- -// InputHttpStream implementation - InputHttpStream::InputHttpStream (const std::string &url) { mp_data = new InputHttpStreamPrivateData (url); @@ -482,7 +449,9 @@ InputHttpStreamPrivateData::read (char *b, size_t n) ec = int (mp_reply->error ()); } - throw HttpErrorException (em, ec, mp_reply); + QByteArray data = mp_reply->readAll (); + + throw HttpErrorException (em, ec, tl::to_string (mp_reply->url ().toString ()), tl::to_string (data.constData (), (int)data.size ())); }