From 6374c77aa8aba34eff1bda0bfd9a498e5ca412c0 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Mon, 12 Aug 2019 22:24:05 +0200 Subject: [PATCH] Read Json error when possible --- src/lyrics/auddlyricsprovider.cpp | 7 ------- src/lyrics/auddlyricsprovider.h | 7 +++---- src/lyrics/jsonlyricsprovider.cpp | 21 +++++++++++++-------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/lyrics/auddlyricsprovider.cpp b/src/lyrics/auddlyricsprovider.cpp index cf41232b3..9f0f69cb3 100644 --- a/src/lyrics/auddlyricsprovider.cpp +++ b/src/lyrics/auddlyricsprovider.cpp @@ -21,14 +21,8 @@ #include #include -#include -#include -#include -#include #include #include -#include -#include #include #include #include @@ -42,7 +36,6 @@ #include "core/closure.h" #include "core/logging.h" #include "core/network.h" -#include "core/utilities.h" #include "jsonlyricsprovider.h" #include "lyricsfetcher.h" #include "auddlyricsprovider.h" diff --git a/src/lyrics/auddlyricsprovider.h b/src/lyrics/auddlyricsprovider.h index 9b160b2fd..1f8944bbb 100644 --- a/src/lyrics/auddlyricsprovider.h +++ b/src/lyrics/auddlyricsprovider.h @@ -25,15 +25,14 @@ #include #include -#include -#include #include -#include -#include #include "jsonlyricsprovider.h" #include "lyricsfetcher.h" +class QNetworkAccessManager; +class QNetworkReply; + class AuddLyricsProvider : public JsonLyricsProvider { Q_OBJECT diff --git a/src/lyrics/jsonlyricsprovider.cpp b/src/lyrics/jsonlyricsprovider.cpp index 28d860e2e..e2da3aaa2 100644 --- a/src/lyrics/jsonlyricsprovider.cpp +++ b/src/lyrics/jsonlyricsprovider.cpp @@ -35,19 +35,24 @@ JsonLyricsProvider::JsonLyricsProvider(const QString &name, QObject *parent) : L QJsonObject JsonLyricsProvider::ExtractJsonObj(QNetworkReply *reply, const quint64 id) { + QString failure_reason; if (reply->error() != QNetworkReply::NoError) { - QString failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()); - Error(id, failure_reason); - return QJsonObject(); + failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()); + if (reply->error() < 200) { + Error(id, failure_reason); + return QJsonObject(); + } } - - if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) { - QString failure_reason = QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()); - Error(id, failure_reason); - return QJsonObject(); + else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) { + failure_reason = QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()); } QByteArray data = reply->readAll(); + if (data.isEmpty()) { + if (failure_reason.isEmpty()) failure_reason = "Empty reply received from server."; + Error(id, failure_reason); + return QJsonObject(); + } QJsonParseError error; QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);