Skip to content

Commit

Permalink
Global exception handler (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolphinspired committed Apr 16, 2021
1 parent f632a99 commit 403c907
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ValheimServerGUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace ValheimServerGUI
{
public static class Program
{
private static IExceptionHandler ExceptionHandler;

/// <summary>
/// The main entry point for the application.
/// </summary>
Expand All @@ -24,12 +26,22 @@ public static void Main()
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

var services = new ServiceCollection();
ConfigureServices(services);
using var serviceProvider = services.BuildServiceProvider();
ExceptionHandler = serviceProvider.GetRequiredService<IExceptionHandler>();

Application.Run(serviceProvider.GetRequiredService<MainWindow>());
try
{
Application.Run(serviceProvider.GetRequiredService<MainWindow>());
}
catch (Exception e)
{
ExceptionHandler.HandleException(e, "Application Run Exception");
}
}

public static void ConfigureServices(IServiceCollection services)
Expand All @@ -53,6 +65,7 @@ public static void ConfigureServices(IServiceCollection services)
services.AddSingleton<IRestClientContext, RestClientContext>();
services.AddSingleton<IIpAddressProvider, IpAddressProvider>();
services.AddSingleton<IGitHubClient, GitHubClient>();
services.AddSingleton<IExceptionHandler, ExceptionHandler>();

// Game & server data
services
Expand All @@ -68,5 +81,15 @@ public static void ConfigureServices(IServiceCollection services)
.AddSingleton<AboutForm>()
.AddTransient<PlayerDetailsForm>();
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
ExceptionHandler.HandleException(e.ExceptionObject as Exception, "Unhandled Exception");
}

private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
ExceptionHandler.HandleException(e.Exception, "Thread Exception");
}
}
}
86 changes: 86 additions & 0 deletions ValheimServerGUI/Tools/ExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ValheimServerGUI.Tools
{
public interface IExceptionHandler
{
void HandleException(Exception e, string additionalMessage = null);
}

public class ExceptionHandler : IExceptionHandler
{
private static readonly string NL = Environment.NewLine;

public void HandleException(Exception e, string additionalMessage = null)
{
if (e == null) return;
additionalMessage ??= "Unhandled Exception";

var message = "An unhandled exception has been thrown, and ValheimServerGUI will be terminated.";
//var stackTrace = string.Join(NL, e.StackTrace.Split(NL).Take(3));
message += $"{NL}{NL}{BuildMessageBody(e, additionalMessage)}";

var result = MessageBox.Show(
message,
additionalMessage,
MessageBoxButtons.OK,
MessageBoxIcon.Error);

//if (result == DialogResult.Yes)
//{
// try
// {
// var body = BuildMessageBody(e, additionalMessage);
// SendEmail("ValheimServerGUI - Automated bug report", additionalMessage);

// MessageBox.Show("Bug report sent. Thank you!", additionalMessage, MessageBoxButtons.OK, MessageBoxIcon.Information);
// }
// catch(Exception e2)
// {
// MessageBox.Show("Failed to send bug report. Sorry!" + e2.Message, additionalMessage, MessageBoxButtons.OK, MessageBoxIcon.Warning);
// }
//}
}

//private void SendEmail(string subject, string body)
//{
// var mailClient = new SmtpClient("");
// var mailMessage = new MailMessage();

// mailMessage.From = new MailAddress("");
// mailMessage.From = new MailAddress("");
// mailMessage.To.Add(new MailAddress(""));

// mailMessage.Subject = subject;
// mailMessage.Body = body;

// mailClient.Send(mailMessage);
// mailMessage.Dispose();
//}

private string BuildMessageBody(Exception e, string additionalMessage)
{
var os = Environment.OSVersion;

var body =
$"{e.GetType().Name}: {e.Message}{NL}" +
$"Timestamp: {DateTime.UtcNow:O}{NL}" +
$"Context: {additionalMessage}{NL}" +
$"Source: {e.Source}{NL}" +
$"TargetSite: {e.TargetSite}{NL}" +
NL +
$"ValheimServerGUI version: {AssemblyHelper.GetApplicationVersion()}{NL}" +
$"OS Version: {os.VersionString}{NL}" +
NL +
$"Stack trace:{NL}{e.StackTrace}";

return body;
}
}
}

0 comments on commit 403c907

Please sign in to comment.