From 3973452ec3e2f4de5595fc27b3d5b86d91937cf3 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 30 Dec 2020 23:50:07 +0100 Subject: [PATCH] Add logging offset for Json parsing errors --- Source/Editor/Managed/ManagedEditor.Internal.cpp | 2 +- Source/Engine/Content/BinaryAsset.cpp | 12 ++++++------ Source/Engine/Content/JsonAsset.cpp | 2 +- Source/Engine/Content/Storage/JsonStorageProxy.cpp | 4 ++-- Source/Engine/Debug/Exceptions/JsonParseException.h | 10 ++++++---- Source/Engine/Level/Actor.cpp | 6 +++--- Source/Engine/Level/Level.cpp | 2 +- 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Source/Editor/Managed/ManagedEditor.Internal.cpp b/Source/Editor/Managed/ManagedEditor.Internal.cpp index 6dc6c8388..597ce0bdf 100644 --- a/Source/Editor/Managed/ManagedEditor.Internal.cpp +++ b/Source/Editor/Managed/ManagedEditor.Internal.cpp @@ -1016,7 +1016,7 @@ public: document.Parse(json.Get(), json.Length()); if (document.HasParseError()) { - Log::JsonParseException(document.GetParseError()); + Log::JsonParseException(document.GetParseError(), document.GetErrorOffset()); DebugLog::ThrowException("Failed to parse Json."); } diff --git a/Source/Engine/Content/BinaryAsset.cpp b/Source/Engine/Content/BinaryAsset.cpp index 0572a50b1..32afb50ca 100644 --- a/Source/Engine/Content/BinaryAsset.cpp +++ b/Source/Engine/Content/BinaryAsset.cpp @@ -118,16 +118,16 @@ void BinaryAsset::GetImportMetadata(String& path, String& username) const } // Parse metadata and try to get import info - rapidjson_flax::Document doc; - doc.Parse((const char*)Metadata.Get(), Metadata.Length()); - if (doc.HasParseError() == false) + rapidjson_flax::Document document; + document.Parse((const char*)Metadata.Get(), Metadata.Length()); + if (document.HasParseError() == false) { - path = JsonTools::GetString(doc, "ImportPath"); - username = JsonTools::GetString(doc, "ImportUsername"); + path = JsonTools::GetString(document, "ImportPath"); + username = JsonTools::GetString(document, "ImportUsername"); } else { - Log::JsonParseException(doc.GetParseError(), GetPath()); + Log::JsonParseException(document.GetParseError(), document.GetErrorOffset(), GetPath()); } } diff --git a/Source/Engine/Content/JsonAsset.cpp b/Source/Engine/Content/JsonAsset.cpp index 4e1f90d70..53909591f 100644 --- a/Source/Engine/Content/JsonAsset.cpp +++ b/Source/Engine/Content/JsonAsset.cpp @@ -128,7 +128,7 @@ Asset::LoadResult JsonAssetBase::loadAsset() Document.Parse(data.Get(), data.Length()); if (Document.HasParseError()) { - Log::JsonParseException(Document.GetParseError()); + Log::JsonParseException(Document.GetParseError(), Document.GetErrorOffset()); return LoadResult::CannotLoadData; } diff --git a/Source/Engine/Content/Storage/JsonStorageProxy.cpp b/Source/Engine/Content/Storage/JsonStorageProxy.cpp index 22934fd51..3efa4b40d 100644 --- a/Source/Engine/Content/Storage/JsonStorageProxy.cpp +++ b/Source/Engine/Content/Storage/JsonStorageProxy.cpp @@ -35,7 +35,7 @@ bool JsonStorageProxy::GetAssetInfo(const StringView& path, Guid& resultId, Stri document.Parse((const char*)fileData.Get(), fileData.Count()); if (document.HasParseError()) { - Log::JsonParseException(document.GetParseError(), String(path)); + Log::JsonParseException(document.GetParseError(), document.GetErrorOffset(), String(path)); return false; } @@ -104,7 +104,7 @@ bool JsonStorageProxy::ChangeId(const StringView& path, const Guid& newId) document.Parse((const char*)fileData.Get(), fileData.Count()); if (document.HasParseError()) { - Log::JsonParseException(document.GetParseError(), String(path)); + Log::JsonParseException(document.GetParseError(), document.GetErrorOffset(), String(path)); return false; } diff --git a/Source/Engine/Debug/Exceptions/JsonParseException.h b/Source/Engine/Debug/Exceptions/JsonParseException.h index 767654ab9..8a8c92649 100644 --- a/Source/Engine/Debug/Exceptions/JsonParseException.h +++ b/Source/Engine/Debug/Exceptions/JsonParseException.h @@ -23,8 +23,9 @@ namespace Log /// Init /// /// Parsing error code. - JsonParseException(ErrorCode error) - : JsonParseException(error, String::Empty) + /// Parsing error location. + JsonParseException(ErrorCode error, size_t offset) + : JsonParseException(error, offset, String::Empty) { } @@ -32,9 +33,10 @@ namespace Log /// Creates default exception with additional data /// /// Parsing error code. + /// Parsing error location. /// Additional information that help describe error - JsonParseException(ErrorCode error, const String& additionalInfo) - : Exception(String::Format(TEXT("Parsing Json failed with error code {0}. {1}"), static_cast(error), GetParseError_En(error)), additionalInfo) + JsonParseException(ErrorCode error, size_t offset, const String& additionalInfo) + : Exception(String::Format(TEXT("Parsing Json failed with error code {0} (offset {2}). {1}"), static_cast(error), GetParseError_En(error), offset), additionalInfo) { } }; diff --git a/Source/Engine/Level/Actor.cpp b/Source/Engine/Level/Actor.cpp index 2dc5c3180..7b04609ae 100644 --- a/Source/Engine/Level/Actor.cpp +++ b/Source/Engine/Level/Actor.cpp @@ -1547,7 +1547,7 @@ bool Actor::FromBytes(const Span& data, Array& output, ISerializeM document.Parse(buffer, bufferSize); if (document.HasParseError()) { - Log::JsonParseException(document.GetParseError()); + Log::JsonParseException(document.GetParseError(), document.GetErrorOffset()); return true; } @@ -1587,7 +1587,7 @@ bool Actor::FromBytes(const Span& data, Array& output, ISerializeM document.Parse(buffer, bufferSize); if (document.HasParseError()) { - Log::JsonParseException(document.GetParseError()); + Log::JsonParseException(document.GetParseError(), document.GetErrorOffset()); return true; } @@ -1699,7 +1699,7 @@ void Actor::FromJson(const StringAnsiView& json) document.Parse(json.Get(), json.Length()); if (document.HasParseError()) { - Log::JsonParseException(document.GetParseError()); + Log::JsonParseException(document.GetParseError(), document.GetErrorOffset()); return; } diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index ae249163b..5d681ac70 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -786,7 +786,7 @@ bool Level::loadScene(const BytesContainer& sceneData, bool autoInitialize, Scen document.Parse(sceneData.Get(), sceneData.Length()); if (document.HasParseError()) { - Log::JsonParseException(document.GetParseError()); + Log::JsonParseException(document.GetParseError(), document.GetErrorOffset()); return true; }