refs #853, JSON exception handling adjustments for airport reader

This commit is contained in:
Klaus Basan
2017-01-05 18:57:18 +01:00
committed by Mathew Sutcliffe
parent ee27ca4d44
commit e956986bf4
3 changed files with 43 additions and 17 deletions

View File

@@ -43,33 +43,57 @@ namespace BlackCore
if (dir.isEmpty() || whatToRead == CEntityFlags::NoEntity) { return false; } if (dir.isEmpty() || whatToRead == CEntityFlags::NoEntity) { return false; }
QTimer::singleShot(0, this, [this, dir, whatToRead]() QTimer::singleShot(0, this, [this, dir, whatToRead]()
{ {
bool s = this->readFromJsonFiles(dir, whatToRead); CStatusMessageList msgs = this->readFromJsonFiles(dir, whatToRead);
Q_UNUSED(s); if (msgs.isFailure())
{
CLogMessage::preformatted(msgs);
}
}); });
return true; return true;
} }
bool CAirportDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead) CStatusMessageList CAirportDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
{ {
QDir directory(dir); const QString fileName = CFileUtils::appendFilePaths(dir, "airports.json");
if (!directory.exists()) { return false; } if (!QFile::exists(fileName))
BlackMisc::Network::CEntityFlags::Entity reallyRead = CEntityFlags::NoEntity; {
return CStatusMessage(this).error("File '%1' does not exist") << fileName;
}
whatToRead &= CEntityFlags::AirportEntity; // can handle these entities
if (whatToRead == CEntityFlags::NoEntity)
{
return CStatusMessage(this).info("'%1' No entity for this reader") << CEntityFlags::flagToString(whatToRead);
}
if (whatToRead.testFlag(CEntityFlags::AirportEntity)) int c = 0;
{ CEntityFlags::Entity reallyRead = CEntityFlags::NoEntity;
QString airportsJson(CFileUtils::readFileToString(CFileUtils::appendFilePaths(directory.absolutePath(), "airports.json"))); const QString airportsJson(CFileUtils::readFileToString(fileName));
if (!airportsJson.isEmpty()) if (!airportsJson.isEmpty())
{
try
{ {
CAirportList airports; CAirportList airports;
airports.convertFromJson(airportsJson); //! \todo catch CJsonException or use convertFromJsonNoThrow airports.convertFromJson(airportsJson);
const int c = airports.size(); c = airports.size();
this->m_airportCache.set(airports); this->m_airportCache.set(airports);
emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFinished, c); emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFinished, c);
reallyRead |= CEntityFlags::AirportEntity; reallyRead |= CEntityFlags::AirportEntity;
} }
catch (const CJsonException &ex)
{
emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFailed, 0);
return ex.toStatusMessage(this, QString("Reading airports from '%1'").arg(fileName));
}
}
if ((reallyRead & CEntityFlags::AirportEntity) == CEntityFlags::AirportEntity)
{
return CStatusMessage(this).info("Written %1 airports from '%2' to cache") << c << fileName;
}
else
{
return CStatusMessage(this).error("Not able to read airports from '%1'") << fileName;
} }
return (reallyRead & CEntityFlags::DistributorLiveryModel) == whatToRead;
} }
CEntityFlags::Entity CAirportDataReader::getSupportedEntities() const CEntityFlags::Entity CAirportDataReader::getSupportedEntities() const

View File

@@ -43,9 +43,10 @@ namespace BlackCore
int getAirportsCount() const; int getAirportsCount() const;
//! Read from static data file //! Read from static data file
bool readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AirportEntity); BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AirportEntity);
//! Read from static data file in background //! Read from static data file in background
//! \return succesfully started?
bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AirportEntity); bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AirportEntity);
// base class overrides // base class overrides

View File

@@ -1209,9 +1209,10 @@ namespace BlackCore
} }
if (s && this->m_airportDataReader) if (s && this->m_airportDataReader)
{ {
s = inBackground ? if (inBackground) { return this->m_airportDataReader->readFromJsonFilesInBackground(dir); }
this->m_airportDataReader->readFromJsonFilesInBackground(dir) : const CStatusMessageList msgs = this->m_airportDataReader->readFromJsonFiles(dir);
this->m_airportDataReader->readFromJsonFiles(dir); if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
return msgs.isSuccess();
} }
return s; return s;
} }