Improve network error messages (#4704)
This commit is contained in:
@@ -155,7 +155,8 @@ void loadUncached(std::shared_ptr<NetworkData> &&data)
|
||||
{
|
||||
postToThread([data] {
|
||||
data->onError_(NetworkResult(
|
||||
{}, NetworkResult::timedoutStatus));
|
||||
NetworkResult::NetworkError::TimeoutError, {},
|
||||
{}));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -218,8 +219,9 @@ void loadUncached(std::shared_ptr<NetworkData> &&data)
|
||||
QString(data->payload_));
|
||||
}
|
||||
// TODO: Should this always be run on the GUI thread?
|
||||
postToThread([data, code = status.toInt(), reply] {
|
||||
data->onError_(NetworkResult(reply->readAll(), code));
|
||||
postToThread([data, status, reply] {
|
||||
data->onError_(NetworkResult(reply->error(), status,
|
||||
reply->readAll()));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -238,7 +240,7 @@ void loadUncached(std::shared_ptr<NetworkData> &&data)
|
||||
auto status =
|
||||
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
||||
|
||||
NetworkResult result(bytes, status.toInt());
|
||||
NetworkResult result(reply->error(), status, bytes);
|
||||
|
||||
DebugCount::increase("http request success");
|
||||
// log("starting {}", data->request_.url().toString());
|
||||
@@ -337,7 +339,8 @@ void loadCached(std::shared_ptr<NetworkData> &&data)
|
||||
|
||||
// XXX: check if bytes is empty?
|
||||
QByteArray bytes = cachedFile.readAll();
|
||||
NetworkResult result(bytes, 200);
|
||||
NetworkResult result(NetworkResult::NetworkError::NoError, QVariant(200),
|
||||
bytes);
|
||||
|
||||
qCDebug(chatterinoHTTP)
|
||||
<< QString("%1 [CACHED] 200 %2")
|
||||
|
||||
@@ -3,15 +3,21 @@
|
||||
#include "common/QLogging.hpp"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QMetaEnum>
|
||||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/error/en.h>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
NetworkResult::NetworkResult(const QByteArray &data, int status)
|
||||
: data_(data)
|
||||
, status_(status)
|
||||
NetworkResult::NetworkResult(NetworkError error, const QVariant &httpStatusCode,
|
||||
QByteArray data)
|
||||
: data_(std::move(data))
|
||||
, error_(error)
|
||||
{
|
||||
if (httpStatusCode.isValid())
|
||||
{
|
||||
this->status_ = httpStatusCode.toInt();
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject NetworkResult::parseJson() const
|
||||
@@ -59,9 +65,21 @@ const QByteArray &NetworkResult::getData() const
|
||||
return this->data_;
|
||||
}
|
||||
|
||||
int NetworkResult::status() const
|
||||
QString NetworkResult::formatError() const
|
||||
{
|
||||
return this->status_;
|
||||
if (this->status_)
|
||||
{
|
||||
return QString::number(*this->status_);
|
||||
}
|
||||
|
||||
const auto *name =
|
||||
QMetaEnum::fromType<QNetworkReply::NetworkError>().valueToKey(
|
||||
this->error_);
|
||||
if (name == nullptr)
|
||||
{
|
||||
return QStringLiteral("unknown error (%1)").arg(this->error_);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -2,14 +2,20 @@
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkReply>
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
class NetworkResult
|
||||
{
|
||||
public:
|
||||
NetworkResult(const QByteArray &data, int status);
|
||||
using NetworkError = QNetworkReply::NetworkError;
|
||||
|
||||
NetworkResult(NetworkError error, const QVariant &httpStatusCode,
|
||||
QByteArray data);
|
||||
|
||||
/// Parses the result as json and returns the root as an object.
|
||||
/// Returns empty object if parsing failed.
|
||||
@@ -20,13 +26,29 @@ public:
|
||||
/// Parses the result as json and returns the document.
|
||||
rapidjson::Document parseRapidJson() const;
|
||||
const QByteArray &getData() const;
|
||||
int status() const;
|
||||
|
||||
static constexpr int timedoutStatus = -2;
|
||||
/// The error code of the reply.
|
||||
/// In case of a successful reply, this will be NoError (0)
|
||||
NetworkError error() const
|
||||
{
|
||||
return this->error_;
|
||||
}
|
||||
|
||||
/// The HTTP status code if a response was received.
|
||||
std::optional<int> status() const
|
||||
{
|
||||
return this->status_;
|
||||
}
|
||||
|
||||
/// Formats the error.
|
||||
/// If a reply is received, returns the HTTP status otherwise, the network error.
|
||||
QString formatError() const;
|
||||
|
||||
private:
|
||||
QByteArray data_;
|
||||
int status_;
|
||||
|
||||
NetworkError error_;
|
||||
std::optional<int> status_;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
Reference in New Issue
Block a user