Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use TaskDialog for reporting exceptions #437

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading