Commit e4e84e78 authored by Volker Krause's avatar Volker Krause
Browse files

Properly handle errors in IVV ASS responses

parent 0909abf7
......@@ -51,7 +51,10 @@ private Q_SLOTS:
QFETCH(QString, inFileName);
QFETCH(QString, outFileName);
const auto result = IvvAssParser::parseLocations(readFile(inFileName));
IvvAssParser p;
const auto result = p.parseLocations(readFile(inFileName));
QVERIFY(!result.empty());
QVERIFY(p.errorMessage.isEmpty());
const auto resultJson = Location::toJson(result);
const auto resultRef = QJsonDocument::fromJson(readFile(outFileName)).array();
if (resultJson != resultRef) {
......@@ -76,7 +79,10 @@ private Q_SLOTS:
QFETCH(QString, inFileName);
QFETCH(QString, outFileName);
const auto result = IvvAssParser::parseStopovers(readFile(inFileName));
IvvAssParser p;
const auto result = p.parseStopovers(readFile(inFileName));
QVERIFY(!result.empty());
QVERIFY(p.errorMessage.isEmpty());
const auto resultJson = Stopover::toJson(result);
const auto resultRef = QJsonDocument::fromJson(readFile(outFileName)).array();
if (resultJson != resultRef) {
......@@ -101,7 +107,10 @@ private Q_SLOTS:
QFETCH(QString, inFileName);
QFETCH(QString, outFileName);
const auto result = IvvAssParser::parseJourneys(readFile(inFileName));
IvvAssParser p;
const auto result = p.parseJourneys(readFile(inFileName));
QVERIFY(!result.empty());
QVERIFY(p.errorMessage.isEmpty());
const auto resultJson = Journey::toJson(result);
const auto resultRef = QJsonDocument::fromJson(readFile(outFileName)).array();
if (resultJson != resultRef) {
......@@ -110,6 +119,25 @@ private Q_SLOTS:
QVERIFY(!resultJson.isEmpty());
QCOMPARE(resultJson, resultRef);
}
void testParseError()
{
{
IvvAssParser p;
const auto res = p.parseLocations(R"({"error":"Ung\u00fcltige Zeit"})");
QVERIFY(!p.errorMessage.isEmpty());
}
{
IvvAssParser p;
const auto res = p.parseStopovers(R"({"error":"Fehlerhaftes Ziel"})");
QVERIFY(!p.errorMessage.isEmpty());
}
{
IvvAssParser p;
const auto res = p.parseJourneys(R"({"error":"Fehlerhafter Start"})");
QVERIFY(!p.errorMessage.isEmpty());
}
}
};
QTEST_GUILESS_MAIN(IvvAssParserTest)
......
......@@ -70,9 +70,14 @@ bool IvvAssBackend::queryLocation(const LocationRequest &req, LocationReply *rep
return;
}
auto result = IvvAssParser::parseLocations(data);
Cache::addLocationCacheEntry(backendId(), reply->request().cacheKey(), result, {});
addResult(reply, std::move(result));
IvvAssParser p;
auto result = p.parseLocations(data);
if (p.errorMessage.isEmpty()) {
Cache::addLocationCacheEntry(backendId(), reply->request().cacheKey(), result, {});
addResult(reply, std::move(result));
} else {
addError(reply, Reply::UnknownError, std::move(p.errorMessage));
}
});
return true;
......@@ -114,8 +119,13 @@ bool IvvAssBackend::queryStopover(const StopoverRequest &req, StopoverReply *rep
return;
}
auto result = IvvAssParser::parseStopovers(data);
addResult(reply, this, std::move(result));
IvvAssParser p;
auto result = p.parseStopovers(data);
if (p.errorMessage.isEmpty()) {
addResult(reply, this, std::move(result));
} else {
addError(reply, Reply::UnknownError, std::move(p.errorMessage));
}
});
return true;
......@@ -163,8 +173,13 @@ bool IvvAssBackend::queryJourney(const JourneyRequest &req, JourneyReply *reply,
return;
}
auto result = IvvAssParser::parseJourneys(data);
addResult(reply, this, std::move(result));
IvvAssParser p;
auto result = p.parseJourneys(data);
if (p.errorMessage.isEmpty()) {
addResult(reply, this, std::move(result));
} else {
addError(reply, Reply::UnknownError, std::move(p.errorMessage));
}
});
return true;
......
......@@ -53,6 +53,9 @@ static Location parseLocation(const QJsonObject &stopObj)
std::vector<Location> IvvAssParser::parseLocations(const QByteArray &data)
{
const auto top = QJsonDocument::fromJson(data).object();
if (!parseError(top)) {
return {};
}
std::vector<Location> locs;
const auto stops = top.value(QLatin1String("stops")).toArray();
......@@ -147,6 +150,9 @@ static Route parseRoute(const QJsonObject &lineObj)
std::vector<Stopover> IvvAssParser::parseStopovers(const QByteArray &data)
{
const auto top = QJsonDocument::fromJson(data).object();
if (!parseError(top)) {
return {};
}
const auto timetable = top.value(QLatin1String("timetable")).toArray();
std::vector<Stopover> stopovers;
......@@ -198,6 +204,9 @@ static std::vector<LoadInfo> parseDemand(const QJsonValue &demand)
std::vector<Journey> IvvAssParser::parseJourneys(const QByteArray &data)
{
const auto top = QJsonDocument::fromJson(data).object();
if (!parseError(top)) {
return {};
}
const auto routes = top.value(QLatin1String("routes")).toArray();
std::vector<Journey> journeys;
......@@ -284,3 +293,9 @@ std::vector<Journey> IvvAssParser::parseJourneys(const QByteArray &data)
return journeys;
}
bool IvvAssParser::parseError(const QJsonObject &top)
{
errorMessage = top.value(QLatin1String("error")).toString();
return errorMessage.isEmpty();
}
......@@ -7,9 +7,12 @@
#ifndef KPUBLICTRANSPORT_IVVASSPARSER_H
#define KPUBLICTRANSPORT_IVVASSPARSER_H
#include <QString>
#include <vector>
class QByteArray;
class QJsonObject;
namespace KPublicTransport {
......@@ -21,9 +24,14 @@ class Stopover;
class IvvAssParser
{
public:
static std::vector<Location> parseLocations(const QByteArray &data);
static std::vector<Stopover> parseStopovers(const QByteArray &data);
static std::vector<Journey> parseJourneys(const QByteArray &data);
std::vector<Location> parseLocations(const QByteArray &data);
std::vector<Stopover> parseStopovers(const QByteArray &data);
std::vector<Journey> parseJourneys(const QByteArray &data);
QString errorMessage;
private:
bool parseError(const QJsonObject &top);
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment