From bc0c0b795034ca377c7cf854374f410b2c668f10 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Tue, 11 Sep 2018 00:20:24 +0200 Subject: [PATCH] Extract error from Json data in API Seeds --- src/lyrics/apiseedslyricsprovider.cpp | 32 +++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/lyrics/apiseedslyricsprovider.cpp b/src/lyrics/apiseedslyricsprovider.cpp index ea6c12ee6..ac5e967ca 100644 --- a/src/lyrics/apiseedslyricsprovider.cpp +++ b/src/lyrics/apiseedslyricsprovider.cpp @@ -119,14 +119,38 @@ void APISeedsLyricsProvider::HandleSearchReply(QNetworkReply *reply, quint64 id, QJsonObject APISeedsLyricsProvider::ExtractJsonObj(QNetworkReply *reply, quint64 id) { - if (reply->error() != QNetworkReply::NoError) { - QString failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()); + QByteArray data; + + if (reply->error() == QNetworkReply::NoError) { + data = reply->readAll(); + } + else { + QString failure_reason; + if (reply->error() < 200) { + QString failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()); + } + else { + // See if there is JSON data containing "error" - then use that instead. + data = reply->readAll(); + QJsonParseError error; + QJsonDocument json_doc = QJsonDocument::fromJson(data, &error); + if (error.error == QJsonParseError::NoError && !json_doc.isNull() && !json_doc.isEmpty() && json_doc.isObject()) { + QJsonObject json_obj = json_doc.object(); + if (!json_obj.isEmpty() && json_obj.contains("error")) { + failure_reason = json_obj["error"].toString(); + } + else { + failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()); + } + } + else { + failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()); + } + } Error(id, failure_reason); return QJsonObject(); } - QByteArray data(reply->readAll()); - QJsonParseError error; QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);