Improve network error messages (#4704)

This commit is contained in:
nerix
2023-07-01 14:59:59 +02:00
committed by GitHub
parent d2f1516818
commit 22b290cb2d
15 changed files with 378 additions and 169 deletions
+26 -4
View File
@@ -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