Skip to content

Commit

Permalink
#115 chore: added error handling for unhandled exceptions and a lifet…
Browse files Browse the repository at this point in the history
…ime service

+ Added an ExtendedException which provides clues about how to solve a specific error
+ The lifetime service manages the lifetime of the web app. It loads the configuration and start the interfaces at the beginning, and stop the interfaces when it's shutting down
+ Renamed TrafficInfo.razor to RealTimeTraffic.razor for better understanding
+ Replaced async void methods with async Task for best practices
+ Fixed a bug with the interface property of AddInterface.razor. It must be set only once, and it was a getter only property which was being generated with every call
  • Loading branch information
joseantmazonsb committed Feb 26, 2022
1 parent 49a441b commit c385c4d
Show file tree
Hide file tree
Showing 21 changed files with 262 additions and 151 deletions.
16 changes: 16 additions & 0 deletions Linguard/Core/Exceptions/ExtendedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Linguard.Core.Exceptions;

public abstract class ExtendedException : Exception {
protected ExtendedException(IEnumerable<string> fixes) {
Fixes = fixes;
}
protected ExtendedException(string message, IEnumerable<string> fixes) : base(message) {
Fixes = fixes;
}
protected ExtendedException(string message, Exception innerException, IEnumerable<string> fixes)
: base(message, innerException) {
Fixes = fixes;
}

public IEnumerable<string> Fixes { get; }
}
1 change: 0 additions & 1 deletion Linguard/Core/Services/DefaultInterfaceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public DefaultInterfaceGenerator(IConfigurationManager configurationManager, IWi
}

public Interface Generate() {

return new Faker<Interface>()
.RuleFor(i => i.Auto, true)
.RuleFor(i => i.Description, f => f.Lorem.Sentence())
Expand Down
18 changes: 13 additions & 5 deletions Linguard/Core/Services/Exceptions/WireguardException.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
namespace Linguard.Core.Services.Exceptions;
using Linguard.Core.Exceptions;
using Linguard.Core.Utils;

public class WireguardException : Exception {
public WireguardException() {}
public WireguardException(string message) : base(message) {}
public WireguardException(string message, Exception innerException) : base(message, innerException) {}
namespace Linguard.Core.Services.Exceptions;

public class WireguardException : ExtendedException {

private static readonly string[] _fixes = {
"Verify that your Wireguard settings are correct.",
$"Ensure the user running {AssemblyInfo.Product} is able to run Wireguard as super user."
};
public WireguardException() : base(_fixes) {}
public WireguardException(string message) : base(message, _fixes) {}
public WireguardException(string message, Exception innerException) : base(message, innerException, _fixes) {}
}
53 changes: 0 additions & 53 deletions Linguard/Web/Middlewares/ConfigurationSetupMiddleware.cs

This file was deleted.

8 changes: 6 additions & 2 deletions Linguard/Web/Pages/AddInterface.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@inject IWireguardService _wireguardService
@inject IWebService _webService;
@inject NotificationService _notificationService
@inject DialogService _dialogService
@inject NavigationManager _navigationManager
@inject IJSRuntime _js
@inject AbstractValidator<Interface> _validator
Expand All @@ -26,7 +25,12 @@
public Guid Id { get; set; }

IConfiguration Configuration => _configurationManager.Configuration;
Interface Iface => _generator.Generate();
Interface Iface { get; set; }

protected override void OnInitialized() {
base.OnInitialized();
Iface = _generator.Generate();
}

}
<PageTitle>@($"{AssemblyInfo.Product} | {Title}")</PageTitle>
Expand Down
2 changes: 1 addition & 1 deletion Linguard/Web/Pages/EditClient.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</RadzenCard>
</RadzenAccordionItem>
<RadzenAccordionItem Text="Traffic data" Icon="swap_vert">
<TrafficInfo Peer="Client"></TrafficInfo>
<RealTimeTraffic Peer="Client"></RealTimeTraffic>
</RadzenAccordionItem>
</Items>
</RadzenAccordion>
Expand Down
6 changes: 3 additions & 3 deletions Linguard/Web/Pages/EditInterface.razor
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
</RadzenCard>
</RadzenAccordionItem>
<RadzenAccordionItem Text="Traffic data" Icon="swap_vert">
<TrafficInfo Peer="Iface"/>
<RealTimeTraffic Peer="Iface"/>
</RadzenAccordionItem>
</Items>
</RadzenAccordion>
Expand All @@ -125,7 +125,7 @@
_navigationManager.NavigateTo($"{nameof(Client).ToLower()}/{client.Id}");
}

async void ConfirmRemoveWireguardPeer(IWireguardPeer peer) {
async Task ConfirmRemoveWireguardPeer(IWireguardPeer peer) {
var peerType = peer.GetType().Name.ToLower();
var remove = await _dialogService.Confirm(
$"Are you sure you want to delete {peer.Name}? This cannot be undone.",
Expand Down Expand Up @@ -174,7 +174,7 @@
$"/{nameof(Interface)}/{Iface.Id}/add-{nameof(Client).ToLower()}");
}

async void ShowQrCode(IWireguardPeer peer) {
async Task ShowQrCode(IWireguardPeer peer) {
var qr = $"data:image/png;base64, {Convert.ToBase64String(_webService.GetQrCode(peer))}" ;
await _dialogService.OpenAsync($"Configuration of {peer.Name}", ds =>
@<div>
Expand Down
41 changes: 0 additions & 41 deletions Linguard/Web/Pages/Error.cshtml

This file was deleted.

23 changes: 0 additions & 23 deletions Linguard/Web/Pages/Error.cshtml.cs

This file was deleted.

4 changes: 2 additions & 2 deletions Linguard/Web/Pages/Wireguard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
_navigationManager.NavigateTo($"/add-{nameof(Interface).ToLower()}");
}

async void ConfirmRemoveWireguardPeer(IWireguardPeer peer) {
async Task ConfirmRemoveWireguardPeer(IWireguardPeer peer) {
var peerType = peer.GetType().Name.ToLower();
var remove = await _dialogService.Confirm(
$"Are you sure you want to delete {peer.Name}? This cannot be undone.",
Expand Down Expand Up @@ -247,7 +247,7 @@
_interfacesGrid.Reload();
}

async void ShowQrCode(IWireguardPeer peer) {
async Task ShowQrCode(IWireguardPeer peer) {
var qr = $"data:image/png;base64, {Convert.ToBase64String(_webService.GetQrCode(peer))}" ;
await _dialogService.OpenAsync($"Configuration of {peer.Name}", ds =>
@<div>
Expand Down
26 changes: 20 additions & 6 deletions Linguard/Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net;
using FluentValidation;
using Linguard.Core.Configuration;
using Linguard.Core.Configuration.Serialization;
Expand All @@ -7,8 +8,8 @@
using Linguard.Core.OS;
using Linguard.Core.Services;
using Linguard.Log;
using Linguard.Web.Middlewares;
using Linguard.Web.Services;
using Microsoft.AspNetCore.Diagnostics;
using QRCoder;
using Radzen;
using IConfiguration = Linguard.Core.Configuration.IConfiguration;
Expand All @@ -34,7 +35,7 @@

builder.Services.AddTransient<IWebService, WebService>();
builder.Services.AddTransient<QRCodeGenerator, QRCodeGenerator>();
builder.Services.AddScoped<ConfigurationSetupMiddleware>();
builder.Services.AddTransient<ILifetimeService, LifetimeService>();

builder.Services.AddScoped<DialogService>();
builder.Services.AddScoped<NotificationService>();
Expand All @@ -43,21 +44,34 @@

var app = builder.Build();

app.Lifetime.ApplicationStarted.Register(() => {
app.Services.GetService<ILifetimeService>()?.OnAppStarted();
});

app.Lifetime.ApplicationStopping.Register(() => {
app.Services.GetService<ILifetimeService>()?.OnAppStopping();
});

app.Lifetime.ApplicationStopped.Register(() => {
app.Services.GetService<ILifetimeService>()?.OnAppStopped();
});

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) {
if (app.Environment.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
else {
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseMiddleware<ConfigurationSetupMiddleware>();
app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
app.Run();
7 changes: 7 additions & 0 deletions Linguard/Web/Services/ILifetimeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Linguard.Web.Services;

public interface ILifetimeService {
void OnAppStarted();
void OnAppStopping();
void OnAppStopped();
}
6 changes: 3 additions & 3 deletions Linguard/Web/Services/IWebService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Linguard.Web.Services;

public interface IWebService {
void Download(string data, string filename);
void DownloadConfiguration();
void DownloadWireguardModel(IWireguardPeer peer);
Task Download(string data, string filename);
Task DownloadConfiguration();
Task DownloadWireguardModel(IWireguardPeer peer);
void RemoveWireguardModel(IWireguardPeer peer);
byte[] GetQrCode(IWireguardPeer peer);
}
Loading

0 comments on commit c385c4d

Please sign in to comment.