Skip to content

Commit

Permalink
use TaskDialog for reporting exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras committed Feb 28, 2024
1 parent e2ebbd0 commit 8ac8d83
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Core
34 changes: 34 additions & 0 deletions Source/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Jamiras.Components;
using Jamiras.Controls;
using Jamiras.Services;
using Jamiras.ViewModels;
using RATools.ViewModels;
using System;
using System.ComponentModel;
Expand Down Expand Up @@ -44,6 +45,9 @@ protected override void OnInitialized(EventArgs e)
CoreServices.RegisterServices();
UIServices.RegisterServices();

var exceptionDispatcher = ServiceRepository.Instance.FindService<IExceptionDispatcher>();
exceptionDispatcher.SetExceptionHandler(UnhandledExceptionHandler);

var dialogService = ServiceRepository.Instance.FindService<IDialogService>();
dialogService.MainWindow = this;
dialogService.DefaultWindowTitle = "RA Tools";
Expand Down Expand Up @@ -72,6 +76,36 @@ protected override void OnInitialized(EventArgs e)
base.OnInitialized(e);
}

private static void UnhandledExceptionHandler(object sender, DispatchExceptionEventArgs e)
{
var innerException = e.Exception;
while (innerException.InnerException != null)
innerException = innerException.InnerException;
var stackTrace = innerException.StackTrace;

try
{
var logService = ServiceRepository.Instance.FindService<ILogService>();
var logger = logService.GetLogger("Jamiras.Core");
logger.WriteError(innerException.Message + "\n" + stackTrace);
}
catch
{
// ignore exception trying to log exception
}

string title = "RA Tools - ";
if (e.IsUnhandled)
title += "Unhandled ";
title += innerException.GetType().Name;

string detail = "More detail may be in the log file.";
if (e.ShouldTerminate)
detail += "\n\nThe application will now terminate.";

TaskDialogViewModel.ShowErrorMessage(innerException.Message, detail, title);
}

protected override void OnClosing(CancelEventArgs e)
{
var viewModel = DataContext as MainWindowViewModel;
Expand Down

0 comments on commit 8ac8d83

Please sign in to comment.