From d5d462cab70fea32d927af87d330b00ed23e218a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Mon, 6 Feb 2023 12:47:45 +0100 Subject: [PATCH] feat: show errors to the user --- .../ViewModels/DocumentViewModel.cs | 49 +++++++++++++------ src/PacketLogger/Views/DocumentView.axaml | 21 ++++++-- src/PacketLogger/Views/PacketLogView.axaml | 1 + 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/PacketLogger/ViewModels/DocumentViewModel.cs b/src/PacketLogger/ViewModels/DocumentViewModel.cs index 3ebebff..523c28d 100644 --- a/src/PacketLogger/ViewModels/DocumentViewModel.cs +++ b/src/PacketLogger/ViewModels/DocumentViewModel.cs @@ -106,7 +106,8 @@ public class DocumentViewModel : Document, INotifyPropertyChanged, IDisposable var openResult = await provider.Open(); if (!openResult.IsSuccess) { - Console.WriteLine("Could not open the file."); + Error = "File could not be opened. " + openResult.ToFullString(); + Loading = false; return; } @@ -127,7 +128,8 @@ public class DocumentViewModel : Document, INotifyPropertyChanged, IDisposable (process.Process, _ctSource.Token, ct); if (!connectionResult.IsDefined(out var connection)) { - Console.WriteLine(connectionResult.ToFullString()); + Error = "An error has occurred upon establishing a connection: " + connectionResult.ToFullString(); + Loading = false; return; } @@ -142,7 +144,8 @@ public class DocumentViewModel : Document, INotifyPropertyChanged, IDisposable if (!contractResult.IsDefined(out var handshakeResponse)) { repository.Remove(connection.Client); - Console.WriteLine(contractResult.ToFullString()); + Error = "An error has occurred upon sending handshake: " + contractResult.ToFullString(); + Loading = false; return; } @@ -150,7 +153,8 @@ public class DocumentViewModel : Document, INotifyPropertyChanged, IDisposable if (!handshakeInitResponse.IsSuccess) { repository.Remove(connection.Client); - Console.WriteLine(handshakeInitResponse.ToFullString()); + Error = "An error has occurred during handshaking: " + handshakeInitResponse.ToFullString(); + Loading = false; return; } @@ -176,16 +180,18 @@ public class DocumentViewModel : Document, INotifyPropertyChanged, IDisposable ); OpenSender = ReactiveCommand.Create - ( - provider => - { - Loading = true; - NestedViewModel = new PacketSenderViewModel(provider); - Title = $"Sender ({provider.Name})"; - Loaded = true; - Loading = false; - } - ); + ( + provider => + { + Loading = true; + NestedViewModel = new PacketSenderViewModel(provider); + Title = $"Sender ({provider.Name})"; + Loaded = true; + Loading = false; + } + ); + + ClearError = ReactiveCommand.Create(() => Error = null); } /// @@ -213,6 +219,16 @@ public class DocumentViewModel : Document, INotifyPropertyChanged, IDisposable /// public bool Loaded { get; private set; } + /// + /// Gets or sets the current error. + /// + public string? Error { get; private set; } + + /// + /// Gets or sets whether there is an error. + /// + public bool HasError => Error is not null; + /// /// Gets the log tab view model. /// @@ -233,6 +249,11 @@ public class DocumentViewModel : Document, INotifyPropertyChanged, IDisposable /// public ReactiveCommand OpenFile { get; } + /// + /// Gets the command to clear the error. + /// + public ReactiveCommand ClearError { get; } + /// /// Gets the command for opening a process / connecting to a process. /// diff --git a/src/PacketLogger/Views/DocumentView.axaml b/src/PacketLogger/Views/DocumentView.axaml index 15239b0..8f8e2fe 100644 --- a/src/PacketLogger/Views/DocumentView.axaml +++ b/src/PacketLogger/Views/DocumentView.axaml @@ -12,12 +12,27 @@ - - - + + + + + + + + + + + +