Extract error from Json data in API Seeds

This commit is contained in:
Jonas Kvinge 2018-09-11 00:20:24 +02:00
parent f202cf6988
commit bc0c0b7950
1 changed files with 28 additions and 4 deletions

View File

@ -119,14 +119,38 @@ void APISeedsLyricsProvider::HandleSearchReply(QNetworkReply *reply, quint64 id,
QJsonObject APISeedsLyricsProvider::ExtractJsonObj(QNetworkReply *reply, quint64 id) { QJsonObject APISeedsLyricsProvider::ExtractJsonObj(QNetworkReply *reply, quint64 id) {
if (reply->error() != QNetworkReply::NoError) { 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()); 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); Error(id, failure_reason);
return QJsonObject(); return QJsonObject();
} }
QByteArray data(reply->readAll());
QJsonParseError error; QJsonParseError error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &error); QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);