Issue #77 Break cyclic dependency statusmessage <-> jsonexception

This commit is contained in:
Mat Sutcliffe
2020-08-27 13:20:34 +01:00
parent 9e0ebef5a1
commit 89659fdeaa
14 changed files with 30 additions and 40 deletions

View File

@@ -116,7 +116,7 @@ namespace BlackCore
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFailed, 0, url); emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFailed, 0, url);
return ex.toStatusMessage(this, QStringLiteral("Reading airports from '%1'").arg(fileName)); return CStatusMessage::fromJsonException(ex, this, QStringLiteral("Reading airports from '%1'").arg(fileName));
} }
} }
} }

View File

@@ -544,7 +544,7 @@ namespace BlackCore
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
emit this->dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFailed, 0, url); emit this->dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFailed, 0, url);
msgs.push_back(ex.toStatusMessage(this, QStringLiteral("Reading countries from '%1'").arg(fileName))); msgs.push_back(CStatusMessage::fromJsonException(ex, this, QStringLiteral("Reading countries from '%1'").arg(fileName)));
} }
} }
} }
@@ -583,7 +583,7 @@ namespace BlackCore
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
emit this->dataRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFailed, 0, url); emit this->dataRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFailed, 0, url);
msgs.push_back(ex.toStatusMessage(this, QStringLiteral("Reading aircraft ICAOs from '%1'").arg(fileName))); msgs.push_back(CStatusMessage::fromJsonException(ex, this, QStringLiteral("Reading aircraft ICAOs from '%1'").arg(fileName)));
} }
} }
} }
@@ -622,7 +622,7 @@ namespace BlackCore
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
emit this->dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFailed, 0, url); emit this->dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFailed, 0, url);
msgs.push_back(ex.toStatusMessage(this, QStringLiteral("Reading airline ICAOs from '%1'").arg(fileName))); msgs.push_back(CStatusMessage::fromJsonException(ex, this, QStringLiteral("Reading airline ICAOs from '%1'").arg(fileName)));
} }
} }
} }
@@ -661,7 +661,7 @@ namespace BlackCore
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
emit this->dataRead(CEntityFlags::AircraftCategoryEntity, CEntityFlags::ReadFailed, 0, url); emit this->dataRead(CEntityFlags::AircraftCategoryEntity, CEntityFlags::ReadFailed, 0, url);
msgs.push_back(ex.toStatusMessage(this, QStringLiteral("Reading categories from '%1'").arg(fileName))); msgs.push_back(CStatusMessage::fromJsonException(ex, this, QStringLiteral("Reading categories from '%1'").arg(fileName)));
} }
} }
} }

View File

@@ -493,7 +493,7 @@ namespace BlackCore
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
emit this->dataRead(CEntityFlags::LiveryEntity, CEntityFlags::ReadFailed, 0, url); emit this->dataRead(CEntityFlags::LiveryEntity, CEntityFlags::ReadFailed, 0, url);
msgs.push_back(ex.toStatusMessage(this, QStringLiteral("Reading liveries from '%1'").arg(fileName))); msgs.push_back(CStatusMessage::fromJsonException(ex, this, QStringLiteral("Reading liveries from '%1'").arg(fileName)));
} }
} }
} }
@@ -533,7 +533,7 @@ namespace BlackCore
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
emit this->dataRead(CEntityFlags::ModelEntity, CEntityFlags::ReadFailed, 0, url); emit this->dataRead(CEntityFlags::ModelEntity, CEntityFlags::ReadFailed, 0, url);
msgs.push_back(ex.toStatusMessage(this, QStringLiteral("Reading models from '%1'").arg(fileName))); msgs.push_back(CStatusMessage::fromJsonException(ex, this, QStringLiteral("Reading models from '%1'").arg(fileName)));
} }
} }
} }
@@ -573,7 +573,7 @@ namespace BlackCore
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
emit this->dataRead(CEntityFlags::DistributorEntity, CEntityFlags::ReadFailed, 0, url); emit this->dataRead(CEntityFlags::DistributorEntity, CEntityFlags::ReadFailed, 0, url);
msgs.push_back(ex.toStatusMessage(this, QStringLiteral("Reading distributors from '%1'").arg(fileName))); msgs.push_back(CStatusMessage::fromJsonException(ex, this, QStringLiteral("Reading distributors from '%1'").arg(fileName)));
} }
} }
} }

View File

@@ -330,7 +330,7 @@ namespace BlackCore
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
return ex.toStatusMessage(this, QStringLiteral("Parsing local setup file '%1'").arg(fn)); return CStatusMessage::fromJsonException(ex, this, QStringLiteral("Parsing local setup file '%1'").arg(fn));
} }
} }
@@ -408,7 +408,7 @@ namespace BlackCore
// we downloaded an unparsable JSON file. // we downloaded an unparsable JSON file.
// as we control those files something is wrong // as we control those files something is wrong
const QString errorMsg = QStringLiteral("Setup file loaded from '%1' cannot be parsed").arg(urlString); const QString errorMsg = QStringLiteral("Setup file loaded from '%1' cannot be parsed").arg(urlString);
const CStatusMessage msg = ex.toStatusMessage(this, errorMsg); const CStatusMessage msg = CStatusMessage::fromJsonException(ex, this, errorMsg);
CLogMessage::preformatted(msg); CLogMessage::preformatted(msg);
emit this->setupLoadingMessages(msg); emit this->setupLoadingMessages(msg);

View File

@@ -152,7 +152,7 @@ namespace BlackGui
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
this->showOverlayMessage(ex.toStatusMessage(this, QStringLiteral("JSON format error. '%1'").arg(fiOtherModelFile.absoluteFilePath()))); this->showOverlayMessage(CStatusMessage::fromJsonException(ex, this, QStringLiteral("JSON format error. '%1'").arg(fiOtherModelFile.absoluteFilePath())));
return false; return false;
} }

View File

@@ -88,7 +88,7 @@ namespace BlackGui
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
ex.toLogMessage(this, "Parse error"); CLogMessage(this).preformatted(CStatusMessage::fromJsonException(ex, this, "Parse error"));
return parts; return parts;
} }
return parts; return parts;

View File

@@ -801,7 +801,7 @@ namespace BlackGui
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
m = ex.toStatusMessage(this, QString("Reading JSON from '%1'").arg(fileName)); m = CStatusMessage::fromJsonException(ex, this, QString("Reading JSON from '%1'").arg(fileName));
break; break;
} }
} }

View File

@@ -630,7 +630,7 @@ namespace BlackMisc
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
const CStatusMessage m = ex.toStatusMessage(&fp, QString("Parsing flight plan from failed.")); const CStatusMessage m = CStatusMessage::fromJsonException(ex, &fp, QString("Parsing flight plan from failed."));
Q_UNUSED(m) Q_UNUSED(m)
} }
return fp; return fp;
@@ -703,7 +703,7 @@ namespace BlackMisc
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
m = ex.toStatusMessage(getLogCategories(), "Parse error in " + fileName); m = CStatusMessage::fromJsonException(ex, getLogCategories(), "Parse error in " + fileName);
if (msgs) { msgs->push_back(m); } if (msgs) { msgs->push_back(m); }
break; break;
} }
@@ -717,7 +717,7 @@ namespace BlackMisc
{ {
if (msgs) if (msgs)
{ {
msgs->push_back(ex.toStatusMessage(getLogCategories(), QStringLiteral("Parsing flight plan from '%1' failed.").arg(fileName))); msgs->push_back(CStatusMessage::fromJsonException(ex, getLogCategories(), QStringLiteral("Parsing flight plan from '%1' failed.").arg(fileName)));
} }
} }
return CFlightPlan(); return CFlightPlan();

View File

@@ -7,9 +7,7 @@
*/ */
#include "blackmisc/jsonexception.h" #include "blackmisc/jsonexception.h"
#include "blackmisc/statusmessage.h" #include "blackmisc/range.h"
#include "blackmisc/logcategorylist.h"
#include "blackmisc/logmessage.h"
#include <QStringBuilder> #include <QStringBuilder>
#include <QThreadStorage> #include <QThreadStorage>
#include <vector> #include <vector>
@@ -28,22 +26,12 @@ namespace BlackMisc
void CJsonException::anchor() void CJsonException::anchor()
{ } { }
CStatusMessage CJsonException::toStatusMessage(const CLogCategoryList &categories, const QString &prefix) const
{
return CStatusMessage(categories).validationError(toString(prefix));
}
QString CJsonException::toString(const QString &prefix) const QString CJsonException::toString(const QString &prefix) const
{ {
if (prefix.isEmpty()) { return QStringLiteral("%1 in '%2'").arg(what()).arg(getStackTrace()); } if (prefix.isEmpty()) { return QStringLiteral("%1 in '%2'").arg(what()).arg(getStackTrace()); }
return QStringLiteral("%1: %2 in '%3'").arg(prefix).arg(what()).arg(getStackTrace()); return QStringLiteral("%1: %2 in '%3'").arg(prefix).arg(what()).arg(getStackTrace());
} }
void CJsonException::toLogMessage(const CLogCategoryList &categories, const QString &prefix) const
{
CLogMessage(categories).validationError(toString(prefix));
}
QString CJsonException::stackString() QString CJsonException::stackString()
{ {
QStringList list; QStringList list;

View File

@@ -34,15 +34,9 @@ namespace BlackMisc
//! Get a stack trace of where in the JSON object tree the error occurred. //! Get a stack trace of where in the JSON object tree the error occurred.
const QString &getStackTrace() const { return m_stack; } const QString &getStackTrace() const { return m_stack; }
//! Get a status message representation.
CStatusMessage toStatusMessage(const CLogCategoryList &categories, const QString &prefix) const;
//! As string info //! As string info
QString toString(const QString &prefix) const; QString toString(const QString &prefix) const;
//! Write a message to the log.
void toLogMessage(const CLogCategoryList &categories, const QString &prefix) const;
private: private:
static QString stackString(); static QString stackString();

View File

@@ -458,7 +458,7 @@ namespace BlackMisc
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
CStatusMessage message = ex.toStatusMessage(this, "Invalid parts packet"); CStatusMessage message = CStatusMessage::fromJsonException(ex, this, "Invalid parts packet");
message.setSeverity(CStatusMessage::SeverityDebug); message.setSeverity(CStatusMessage::SeverityDebug);
CLogMessage::preformatted(message); CLogMessage::preformatted(message);
} }

View File

@@ -348,6 +348,11 @@ namespace BlackMisc
return m; return m;
} }
CStatusMessage CStatusMessage::fromJsonException(const CJsonException &ex, const CLogCategoryList &categories, const QString &prefix)
{
return CStatusMessage(categories).validationError(ex.toString(prefix));
}
void CStatusMessage::registerMetadata() void CStatusMessage::registerMetadata()
{ {
CValueObject<CStatusMessage>::registerMetadata(); CValueObject<CStatusMessage>::registerMetadata();

View File

@@ -510,6 +510,9 @@ namespace BlackMisc
//! Object from JSON //! Object from JSON
static CStatusMessage fromDatabaseJson(const QJsonObject &json); static CStatusMessage fromDatabaseJson(const QJsonObject &json);
//! Object from JSON exception message
static CStatusMessage fromJsonException(const CJsonException &ex, const CLogCategoryList &categories, const QString &prefix);
//! \copydoc BlackMisc::CValueObject::registerMetadata //! \copydoc BlackMisc::CValueObject::registerMetadata
static void registerMetadata(); static void registerMetadata();
@@ -543,7 +546,7 @@ namespace BlackMisc
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
return ex.toStatusMessage(categories, prefix); return CStatusMessage::fromJsonException(ex, categories, prefix);
} }
return {}; return {};
} }
@@ -558,7 +561,7 @@ namespace BlackMisc
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
return ex.toStatusMessage(categories, prefix); return CStatusMessage::fromJsonException(ex, categories, prefix);
} }
return {}; return {};
} }

View File

@@ -287,7 +287,7 @@ namespace BlackMisc
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
return ex.toStatusMessage(categories, prefix); return CStatusMessage::fromJsonException(ex, categories, prefix);
} }
return {}; return {};
} }
@@ -355,7 +355,7 @@ namespace BlackMisc
} }
catch (const CJsonException &ex) catch (const CJsonException &ex)
{ {
return ex.toStatusMessage(categories, prefix); return CStatusMessage::fromJsonException(ex, categories, prefix);
} }
return {}; return {};
} }