diff --git a/src/CalcViewModel/Common/TraceLogger.cpp b/src/CalcViewModel/Common/TraceLogger.cpp index 9d4bac78..b1017e6f 100644 --- a/src/CalcViewModel/Common/TraceLogger.cpp +++ b/src/CalcViewModel/Common/TraceLogger.cpp @@ -345,10 +345,9 @@ namespace CalculatorApp TraceLoggingCommon::GetInstance()->LogLevel2Event(StringReference(EVENT_NAME_RECALL_RESTORE), fields); } - void TraceLogger::LogRecallError(CalculatorApp::ViewModel::Common::ViewMode mode, Platform::String^ message) + void TraceLogger::LogRecallError(Platform::String^ message) { auto fields = ref new LoggingFields(); - fields->AddString(StringReference(CALC_MODE), NavCategoryStates::GetFriendlyName(mode)); fields->AddString(StringReference(L"FunctionName"), L"Recall"); fields->AddString(StringReference(L"Message"), message); TraceLoggingCommon::GetInstance()->LogLevel2Event(StringReference(EVENT_NAME_EXCEPTION), fields); diff --git a/src/CalcViewModel/Common/TraceLogger.h b/src/CalcViewModel/Common/TraceLogger.h index 25e57eea..0c70b0a9 100644 --- a/src/CalcViewModel/Common/TraceLogger.h +++ b/src/CalcViewModel/Common/TraceLogger.h @@ -87,7 +87,7 @@ namespace CalculatorApp::ViewModel::Common void LogPlatformExceptionInfo(CalculatorApp::ViewModel::Common::ViewMode mode, Platform::String ^ functionName, Platform::String ^ message, int hresult); void LogRecallSnapshot(CalculatorApp::ViewModel::Common::ViewMode mode); void LogRecallRestore(CalculatorApp::ViewModel::Common::ViewMode mode); - void LogRecallError(CalculatorApp::ViewModel::Common::ViewMode mode, Platform::String ^ message); + void LogRecallError(Platform::String ^ message); internal: void LogPlatformException(CalculatorApp::ViewModel::Common::ViewMode mode, Platform::String ^ functionName, Platform::Exception ^ e); diff --git a/src/Calculator/Common/LaunchArguments.cs b/src/Calculator/Common/LaunchArguments.cs index fc887356..27c8853d 100644 --- a/src/Calculator/Common/LaunchArguments.cs +++ b/src/Calculator/Common/LaunchArguments.cs @@ -5,6 +5,7 @@ using Windows.ApplicationModel.Activation; using CalculatorApp.ViewModel.Snapshot; using CalculatorApp.JsonUtils; +using CalculatorApp.ViewModel.Common; namespace CalculatorApp { @@ -42,8 +43,9 @@ namespace CalculatorApp var snapshot = JsonSerializer.Deserialize(jsonStr); return new SnapshotLaunchArguments { HasError = false, Snapshot = snapshot.Value }; } - catch (Exception) + catch (Exception ex) { + TraceLogger.GetInstance().LogRecallError($"Error occurs during the deserialization of Snapshot. Exception: {ex}"); return new SnapshotLaunchArguments { HasError = true }; } } diff --git a/src/Calculator/Utils/DeflateUtils.cs b/src/Calculator/Utils/DeflateUtils.cs index 3d4b2fbd..b5a511d1 100644 --- a/src/Calculator/Utils/DeflateUtils.cs +++ b/src/Calculator/Utils/DeflateUtils.cs @@ -23,10 +23,10 @@ namespace CalculatorApp public static string Decompress(byte[] data) { using (var srcStream = new MemoryStream(data)) - using (var deflateStream = new DeflateStream(srcStream, CompressionMode.Decompress)) + using (var inflater = new DeflateStream(srcStream, CompressionMode.Decompress)) using (var resultStream = new MemoryStream()) { - deflateStream.CopyTo(resultStream); + inflater.CopyTo(resultStream); byte[] decompressed = resultStream.ToArray(); return Encoding.UTF8.GetString(decompressed); } diff --git a/src/Calculator/Utils/JsonUtils.cs b/src/Calculator/Utils/JsonUtils.cs index 0a49b430..86fbc953 100644 --- a/src/Calculator/Utils/JsonUtils.cs +++ b/src/Calculator/Utils/JsonUtils.cs @@ -65,6 +65,7 @@ namespace CalculatorApp.JsonUtils get => Value.Command; set => Value.Command = value; } + public BinaryCommandAlias() => Value = new BinaryCommand(); public BinaryCommandAlias(BinaryCommand value) => Value = value; } @@ -114,6 +115,7 @@ namespace CalculatorApp.JsonUtils get => Value.Command; set => Value.Command = value; } + public ParenthesesAlias() => Value = new Parentheses(); public ParenthesesAlias(Parentheses value) => Value = value; } @@ -129,27 +131,25 @@ namespace CalculatorApp.JsonUtils get => Value.Tokens.Select(x => new CalcManagerHistoryTokenAlias(x)); set => Value.Tokens = value.Select(Helpers.MapHistoryToken).ToList(); } - [JsonPropertyName("c")] public IEnumerable Commands { get => Value.Commands.Select(Helpers.MapCommandAlias); set => Value.Commands = value.Select(Helpers.MapCommandAlias).ToList(); } - [JsonPropertyName("e")] public string Expression { get => Value.Expression; set => Value.Expression = value; } - [JsonPropertyName("r")] public string Result { get => Value.Result; set => Value.Result = value; } + public CalcManagerHistoryItemAlias() => Value = new CalcManagerHistoryItem(); public CalcManagerHistoryItemAlias(CalcManagerHistoryItem value) => Value = value; } diff --git a/src/Calculator/Views/MainPage.xaml.cs b/src/Calculator/Views/MainPage.xaml.cs index 7f3b10cf..de0a5194 100644 --- a/src/Calculator/Views/MainPage.xaml.cs +++ b/src/Calculator/Views/MainPage.xaml.cs @@ -77,9 +77,9 @@ namespace CalculatorApp var json = JsonSerializer.Serialize(new ApplicationSnapshotAlias(Model.Snapshot)); embeddedData = Convert.ToBase64String(DeflateUtils.Compress(json)); } - catch (Exception) + catch (Exception ex) { - // TODO: trace errors + TraceLogger.GetInstance().LogRecallError($"Error occurs during the serialization of Snapshot. Exception: {ex}"); deferral.Complete(); return; } @@ -179,8 +179,18 @@ namespace CalculatorApp } else if (e.Parameter is SnapshotLaunchArguments snapshotArgs) { - Model.RestoreFromSnapshot(snapshotArgs.Snapshot); Model.Initialize(initialMode); + if (!snapshotArgs.HasError) + { + Model.RestoreFromSnapshot(snapshotArgs.Snapshot); + TraceLogger.GetInstance().LogRecallRestore((ViewMode)snapshotArgs.Snapshot.Mode); + } + else + { + _ = Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, + async () => await ShowSnapshotLaunchErrorAsync()); + TraceLogger.GetInstance().LogRecallError("OnNavigatedTo:Found errors."); + } } else {