refs #884 Use CJsonException instead of CLogMessage when CVariant::convertFromJson fails.

This commit is contained in:
Mathew Sutcliffe
2017-02-28 17:05:35 +00:00
parent 01a84252db
commit baf5c62502

View File

@@ -163,10 +163,10 @@ namespace BlackMisc
int typeId = QMetaType::type(qPrintable(typeName)); int typeId = QMetaType::type(qPrintable(typeName));
QJsonValue value = json.value("value"); QJsonValue value = json.value("value");
if (value.isUndefined() && typeId != QVariant::Invalid) { throw CJsonException("Missing 'value'"); } if (value.isUndefined()) { throw CJsonException("Missing 'value'"); }
switch (typeId) switch (typeId)
{ {
case QVariant::Invalid: CLogMessage(this).warning("Invalid type for fromJson: %1") << typeName; m_v.clear(); break; case QVariant::Invalid: throw CJsonException("Type not recognized by QMetaType");
case QVariant::Int: m_v.setValue(value.toInt()); break; case QVariant::Int: m_v.setValue(value.toInt()); break;
case QVariant::UInt: m_v.setValue<uint>(value.toInt()); break; case QVariant::UInt: m_v.setValue<uint>(value.toInt()); break;
case QVariant::Bool: m_v.setValue(value.toBool()); break; case QVariant::Bool: m_v.setValue(value.toBool()); break;
@@ -195,17 +195,17 @@ namespace BlackMisc
m_v.setValue(value.toString()); m_v.setValue(value.toString());
if (! m_v.convert(typeId)) if (! m_v.convert(typeId))
{ {
CLogMessage(this).warning("Failed to convert from JSON string"); throw CJsonException("Failed to convert from JSON string");
} }
} }
else else
{ {
CLogMessage(this).warning("Unsupported CVariant type for fromJson: %1 (%2)") << typeName << typeId; throw CJsonException("Type not supported by convertFromJson");
} }
} }
catch (const Private::CVariantException &ex) catch (const Private::CVariantException &ex)
{ {
CLogMessage(this).debug() << ex.what(); throw CJsonException(ex.what());
} }
} }
} }
@@ -257,6 +257,8 @@ namespace BlackMisc
auto *meta = Private::getValueObjectMetaInfo(typeId); auto *meta = Private::getValueObjectMetaInfo(typeId);
if (meta) if (meta)
{
try
{ {
QJsonValue value = json.value("value"); QJsonValue value = json.value("value");
if (value.isUndefined()) { throw CJsonException("Missing 'value'"); } if (value.isUndefined()) { throw CJsonException("Missing 'value'"); }
@@ -265,6 +267,11 @@ namespace BlackMisc
m_v = QVariant(typeId, nullptr); m_v = QVariant(typeId, nullptr);
meta->convertFromMemoizedJson(value.toObject(), data()); meta->convertFromMemoizedJson(value.toObject(), data());
} }
catch (const Private::CVariantException &ex)
{
throw CJsonException(ex.what());
}
}
else else
{ {
convertFromJson(json); convertFromJson(json);