NetworkRequest onError now uses NetworkResult

This commit is contained in:
fourtf
2019-09-19 19:03:50 +02:00
parent 986694e4bc
commit 758a6bb41c
10 changed files with 60 additions and 72 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ class Outcome;
class NetworkResult;
using NetworkSuccessCallback = std::function<Outcome(NetworkResult)>;
using NetworkErrorCallback = std::function<bool(int)>;
using NetworkErrorCallback = std::function<void(NetworkResult)>;
using NetworkReplyCreatedCallback = std::function<void(QNetworkReply *)>;
enum class NetworkRequestType {
+16 -12
View File
@@ -130,15 +130,16 @@ void loadUncached(const std::shared_ptr<NetworkData> &data)
if (data->timer_->isActive())
{
QObject::connect(data->timer_, &QTimer::timeout, worker,
[reply, data]() {
log("Aborted!");
reply->abort();
if (data->onError_)
{
data->onError_(-2);
}
});
QObject::connect(
data->timer_, &QTimer::timeout, worker, [reply, data]() {
log("Aborted!");
reply->abort();
if (data->onError_)
{
data->onError_(
NetworkResult({}, NetworkResult::timedoutStatus));
}
});
}
if (data->onReplyCreated_)
@@ -157,7 +158,7 @@ void loadUncached(const std::shared_ptr<NetworkData> &data)
{
if (data->onError_)
{
data->onError_(reply->error());
data->onError_(NetworkResult({}, reply->error()));
}
return;
}
@@ -165,7 +166,10 @@ void loadUncached(const std::shared_ptr<NetworkData> &data)
QByteArray bytes = reply->readAll();
writeToCache(data, bytes);
NetworkResult result(bytes);
auto status =
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
NetworkResult result(bytes, status.toInt());
DebugCount::increase("http request success");
// log("starting {}", data->request_.url().toString());
@@ -223,7 +227,7 @@ void loadCached(const std::shared_ptr<NetworkData> &data)
{
// XXX: check if bytes is empty?
QByteArray bytes = cachedFile.readAll();
NetworkResult result(bytes);
NetworkResult result(bytes, 200);
if (data->onSuccess_)
{
+7 -1
View File
@@ -8,8 +8,9 @@
namespace chatterino {
NetworkResult::NetworkResult(const QByteArray &data)
NetworkResult::NetworkResult(const QByteArray &data, int status)
: data_(data)
, status_(status)
{
}
@@ -57,4 +58,9 @@ const QByteArray &NetworkResult::getData() const
return this->data_;
}
int NetworkResult::status() const
{
return this->status_;
}
} // namespace chatterino
+5 -1
View File
@@ -8,7 +8,7 @@ namespace chatterino {
class NetworkResult
{
public:
NetworkResult(const QByteArray &data);
NetworkResult(const QByteArray &data, int status);
/// Parses the result as json and returns the root as an object.
/// Returns empty object if parsing failed.
@@ -19,8 +19,12 @@ 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;
private:
int status_;
QByteArray data_;
};