Skip to content

Commit

Permalink
Don't throw error on empty reply
Browse files Browse the repository at this point in the history
Sometimes, the server reply can be empty and still valid. This is for
example the case with time table replies when there are no departures
from a station (or at least not any departures matching the selected
direction).

Fixes smurfy#267.
  • Loading branch information
Erik Lundin committed Dec 13, 2018
1 parent b43623a commit 57d101c
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/parser/parser_resrobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,17 @@ void ParserResRobot::parseTimeTable(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
QVariant reply = parseJson(allData);
if (!reply.isValid()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
}

QVariantMap replyMap = reply.toMap();
QVariantList departures;
if (timetableSearchMode == Arrival)
departures = doc.value("Arrival").toList();
departures = replyMap.value("Arrival").toList();
else
departures = doc.value("Departure").toList();
departures = replyMap.value("Departure").toList();
TimetableEntriesList timetable;
foreach (QVariant departureData, departures) {
TimetableEntry resultItem;
Expand Down Expand Up @@ -439,12 +439,13 @@ void ParserResRobot::parseStationsByName(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
QVariant reply = parseJson(allData);
if (!reply.isValid()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
}
QVariantList stations = doc.value("StopLocation").toList();
QVariantMap replyMap = reply.toMap();
QVariantList stations = replyMap.value("StopLocation").toList();
StationsList result;
foreach (QVariant stationData, stations) {
const QVariantMap& station = stationData.toMap();
Expand All @@ -464,13 +465,13 @@ void ParserResRobot::parseStationsByCoordinates(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
QVariant reply = parseJson(allData);
if (!reply.isValid()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
}

QVariantList stations = doc.value("StopLocation").toList();
QVariantMap replyMap = reply.toMap();
QVariantList stations = replyMap.value("StopLocation").toList();
StationsList result;
foreach (QVariant stationData, stations) {
const QVariantMap& station = stationData.toMap();
Expand All @@ -490,15 +491,15 @@ void ParserResRobot::parseSearchJourney(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
QVariant reply = parseJson(allData);
if (!reply.isValid()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
}

searchEarlierReference = doc.value("scrB").toString();
searchLaterReference = doc.value("scrF").toString();
QVariantList journeyListData = doc.value("Trip").toList();
QVariantMap replyMap = reply.toMap();
searchEarlierReference = replyMap.value("scrB").toString();
searchLaterReference = replyMap.value("scrF").toString();
QVariantList journeyListData = replyMap.value("Trip").toList();

cachedResults.clear();

Expand Down

0 comments on commit 57d101c

Please sign in to comment.