Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Improve error handling (#21)

Leendert de Borst9a20c9c0b378
diff --git a/src/AliasVault.WebApp/Components/Alerts/AlertMessageError.razor b/src/AliasVault.WebApp/Components/Alerts/AlertMessageError.razor
index f68155415..09782361f 100644
--- a/src/AliasVault.WebApp/Components/Alerts/AlertMessageError.razor
+++ b/src/AliasVault.WebApp/Components/Alerts/AlertMessageError.razor
@@ -6,7 +6,7 @@
return;
}
-<div class="p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-50 dark:bg-gray-800 dark:text-red-400" role="alert">
+<div class="p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-50 dark:bg-gray-800 dark:text-red-400 border-2" role="alert">
@Message
</div>
diff --git a/src/AliasVault.WebApp/Components/Alerts/GlobalNotificationDisplay.razor b/src/AliasVault.WebApp/Components/Alerts/GlobalNotificationDisplay.razor
index d3632b64c..a684c9ffd 100644
--- a/src/AliasVault.WebApp/Components/Alerts/GlobalNotificationDisplay.razor
+++ b/src/AliasVault.WebApp/Components/Alerts/GlobalNotificationDisplay.razor
@@ -17,7 +17,7 @@
}
@code {
- private Dictionary<string, string> Messages { get; set; } = new();
+ private List<KeyValuePair<string, string>> Messages { get; set; } = new();
private bool _onChangeSubscribed = false;
/// <inheritdoc />
@@ -54,17 +54,17 @@
var newMessages = GlobalNotificationService.GetMessagesForDisplay();
foreach (var message in newMessages)
{
- if (!Messages.ContainsValue(message.Value))
+ if (!Messages.Any(m => m.Key == message.Key && m.Value == message.Value))
{
- Messages.Add(message.Key, message.Value);
+ Messages.Add(message);
}
}
// Remove messages that are no longer in the GlobalNotificationService and have already been displayed.
- var keysToRemove = Messages.Keys.Except(newMessages.Keys).ToList();
- foreach (var key in keysToRemove)
+ var messagesToRemove = Messages.Where(m => !newMessages.Any(nm => nm.Key == m.Key && nm.Value == m.Value)).ToList();
+ foreach (var message in messagesToRemove)
{
- Messages.Remove(key);
+ Messages.Remove(message);
}
StateHasChanged();
diff --git a/src/AliasVault.WebApp/Components/Breadcrumb.razor b/src/AliasVault.WebApp/Components/Breadcrumb.razor
index 07aa0341d..f06fc858e 100644
--- a/src/AliasVault.WebApp/Components/Breadcrumb.razor
+++ b/src/AliasVault.WebApp/Components/Breadcrumb.razor
@@ -1,6 +1,5 @@
-@using AliasVault.WebApp.Components.Models
+@inherits ComponentBase
@using Microsoft.IdentityModel.Tokens
-@inherits ComponentBase
<nav class="flex mb-5" aria-label="RecentEmails">
<ol class="inline-flex items-center space-x-1 text-sm font-medium md:space-x-2">
@@ -33,11 +32,16 @@
}
</ol>
</nav>
+<GlobalNotificationDisplay />
@code {
+ /// <summary>
+ /// Gets or sets the list of breadcrumb items.
+ /// </summary>
[Parameter]
- public List<BreadcrumbItem> BreadcrumbItems { get; set; } = new List<BreadcrumbItem>();
+ public List<BreadcrumbItem> BreadcrumbItems { get; set; } = new();
+ /// <inheritdoc />
protected override void OnInitialized()
{
base.OnInitialized();
diff --git a/src/AliasVault.WebApp/Pages/Aliases/AddEdit.razor b/src/AliasVault.WebApp/Pages/Aliases/AddEdit.razor
index 8536f45c0..2e9e3a261 100644
--- a/src/AliasVault.WebApp/Pages/Aliases/AddEdit.razor
+++ b/src/AliasVault.WebApp/Pages/Aliases/AddEdit.razor
@@ -195,7 +195,9 @@ else
{
if (Id is null)
{
- Navigation.NavigateTo("/404");
+ // Error loading alias.
+ GlobalNotificationService.AddErrorMessage("This alias does not exist (anymore). Please try again.");
+ NavigationManager.NavigateTo("/", false, true);
return;
}
@@ -203,7 +205,9 @@ else
var alias = await AliasService.LoadAliasAsync(Id.Value);
if (alias is null)
{
- Navigation.NavigateTo("/404");
+ // Error loading alias.
+ GlobalNotificationService.AddErrorMessage("This alias does not exist (anymore). Please try again.");
+ NavigationManager.NavigateTo("/", false, true);
return;
}
@@ -293,25 +297,34 @@ else
{
if (Id is not null)
{
- await AliasService.UpdateAliasAsync(Obj, Id.Value);
- GlobalNotificationService.AddSuccessMessage("Alias updated successfully.");
+ Id = await AliasService.UpdateAliasAsync(Obj, Id.Value);
}
}
else
{
Id = await AliasService.InsertAliasAsync(Obj);
- GlobalNotificationService.AddSuccessMessage("Alias created successfully.");
}
IsSaving = false;
StateHasChanged();
- if (Id == Guid.Empty)
+ if (Id is null || Id == Guid.Empty)
{
- // Error saving, do not navigate.
+ // Error saving.
+ GlobalNotificationService.AddErrorMessage("Error saving alias. Please try again.", true);
return;
}
+ // No error, add success message.
+ if (EditMode)
+ {
+ GlobalNotificationService.AddSuccessMessage("Alias updated successfully.");
+ }
+ else
+ {
+ GlobalNotificationService.AddSuccessMessage("Alias created successfully.");
+ }
+
Navigation.NavigateTo("/alias/" + Id);
}
}
diff --git a/src/AliasVault.WebApp/Pages/Aliases/Delete.razor b/src/AliasVault.WebApp/Pages/Aliases/Delete.razor
index 7af6464a2..45f04811a 100644
--- a/src/AliasVault.WebApp/Pages/Aliases/Delete.razor
+++ b/src/AliasVault.WebApp/Pages/Aliases/Delete.razor
@@ -80,10 +80,12 @@ else
{
if (Obj is null)
{
+ GlobalNotificationService.AddErrorMessage("Error deleting. Alias not found.", true);
return;
}
await AliasService.DeleteAliasAsync(Id);
+ GlobalNotificationService.AddSuccessMessage("Alias successfully deleted.");
NavigationManager.NavigateTo("/");
}
diff --git a/src/AliasVault.WebApp/Pages/Aliases/View.razor b/src/AliasVault.WebApp/Pages/Aliases/View.razor
index b1ace6222..b317e4ba0 100644
--- a/src/AliasVault.WebApp/Pages/Aliases/View.razor
+++ b/src/AliasVault.WebApp/Pages/Aliases/View.razor
@@ -16,7 +16,6 @@ else
<div class="grid grid-cols-2 px-4 pt-6 md:grid-cols-3 lg:gap-4 dark:bg-gray-900">
<div class="mb-4 col-span-full xl:mb-2">
<Breadcrumb BreadcrumbItems="BreadcrumbItems"/>
- <GlobalNotificationDisplay />
<div class="flex items-center justify-between">
<h1 class="text-xl font-semibold text-gray-900 sm:text-2xl dark:text-white">View alias</h1>
@@ -147,6 +146,15 @@ else
// Load the aliases from the webapi via AliasService.
Alias = await AliasService.LoadAliasAsync(Id);
+
+ if (Alias is null)
+ {
+ // Error loading alias.
+ GlobalNotificationService.AddErrorMessage("This alias does not exist (anymore). Please try again.");
+ NavigationManager.NavigateTo("/", false, true);
+ return;
+ }
+
AliasEmail = Alias?.Identity.EmailPrefix + "@landmail.nl";
IsLoading = false;
diff --git a/src/AliasVault.WebApp/Pages/Home.razor b/src/AliasVault.WebApp/Pages/Home.razor
index 5a8c0ae00..5c7ebabae 100644
--- a/src/AliasVault.WebApp/Pages/Home.razor
+++ b/src/AliasVault.WebApp/Pages/Home.razor
@@ -54,8 +54,7 @@
if (aliasListEntries is null)
{
// Error loading aliases.
- IsLoading = false;
- StateHasChanged();
+ GlobalNotificationService.AddErrorMessage("Failed to load aliases.", true);
return;
}
diff --git a/src/AliasVault.WebApp/Services/AliasService.cs b/src/AliasVault.WebApp/Services/AliasService.cs
index b7643ce5a..0c4cfec85 100644
--- a/src/AliasVault.WebApp/Services/AliasService.cs
+++ b/src/AliasVault.WebApp/Services/AliasService.cs
@@ -97,7 +97,7 @@ public class AliasService(HttpClient httpClient)
}
catch
{
- return new List<AliasListEntry>();
+ return null;
}
}
diff --git a/src/AliasVault.WebApp/Services/GlobalNotificationService.cs b/src/AliasVault.WebApp/Services/GlobalNotificationService.cs
index eb81ac32d..11ab08181 100644
--- a/src/AliasVault.WebApp/Services/GlobalNotificationService.cs
+++ b/src/AliasVault.WebApp/Services/GlobalNotificationService.cs
@@ -20,16 +20,23 @@ public class GlobalNotificationService
public event Action? OnChange;
/// <summary>
- /// Gets or sets success messages that should be displayed to the user. A default set of success messages is added in the parent OnInitialized method.
+ /// Gets or sets success messages that should be displayed to the user.
/// </summary>
- protected List<string> SuccessMessages { get; set; } = new List<string>();
+ protected List<string> SuccessMessages { get; set; } = new();
+
+ /// <summary>
+ /// Gets or sets error messages that should be displayed to the user.
+ /// </summary>
+ protected List<string> ErrorMessages { get; set; } = new();
/// <summary>
/// Adds a success message to the list of messages that should be displayed to the user.
/// </summary>
/// <param name="message">The message to add.</param>
- /// <param name="notifyStateChanged">Whether to notify state change to subscribers. Defaults to true.</param>
- public void AddSuccessMessage(string message, bool notifyStateChanged = true)
+ /// <param name="notifyStateChanged">Whether to notify state change to subscribers. Defaults to false.
+ /// Set this to true if you want to show the added message instantly instead of waiting for the notification
+ /// display to rerender (e.g. after navigation).</param>
+ public void AddSuccessMessage(string message, bool notifyStateChanged = false)
{
SuccessMessages.Add(message);
@@ -40,21 +47,45 @@ public class GlobalNotificationService
}
}
+ /// <summary>
+ /// Adds an error message to the list of messages that should be displayed to the user.
+ /// </summary>
+ /// <param name="message">The message to add.</param>
+ /// <param name="notifyStateChanged">Whether to notify state change to subscribers. Defaults to false.
+ /// Set this to true if you want to show the added message instantly instead of waiting for the notification
+ /// display to rerender (e.g. after navigation).</param>
+ public void AddErrorMessage(string message, bool notifyStateChanged = false)
+ {
+ ErrorMessages.Add(message);
+
+ // Notify subscribers that a message has been added.
+ if (notifyStateChanged)
+ {
+ NotifyStateChanged();
+ }
+ }
+
/// <summary>
/// Returns a dictionary with messages that should be displayed to the user. After this method is called,
/// the messages are automatically cleared.
/// </summary>
/// <returns>Dictionary with messages that are ready to be displayed on the next page load.</returns>
- public Dictionary<string, string> GetMessagesForDisplay()
+ public List<KeyValuePair<string, string>> GetMessagesForDisplay()
{
- var messages = new Dictionary<string, string>();
+ var messages = new List<KeyValuePair<string, string>>();
foreach (var message in SuccessMessages)
{
- messages.Add("success", message);
+ messages.Add(new KeyValuePair<string, string>("success", message));
+ }
+
+ foreach (var message in ErrorMessages)
+ {
+ messages.Add(new KeyValuePair<string, string>("error", message));
}
// Clear messages
SuccessMessages.Clear();
+ ErrorMessages.Clear();
return messages;
}