forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trap and route as many exceptions as possible to the central handler. For exceptions coming from the main thread it is possible to ignore those and continue the app's execution, but for unhandled exceptions coming from background threads the app will terminate. The main focus of this work is to intercept exceptions originated from `Executable` (i.e. executing git commands, etc.), and user scripts. However it is not limited in this scope. Resolves gitextensions#7795
- Loading branch information
Showing
11 changed files
with
285 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
|
||
#nullable enable | ||
|
||
namespace GitCommands | ||
{ | ||
/// <summary> | ||
/// Represents errors that occur during execution of an external operation, | ||
/// e.g. running a git operation or launching an external process. | ||
/// </summary> | ||
public class ExternalOperationException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ExternalOperationException"/> class with a specified parameters | ||
/// and a reference to the inner exception that is the cause of this exception. | ||
/// </summary> | ||
/// <param name="command">The command that led to the exception.</param> | ||
/// <param name="arguments">The command arguments.</param> | ||
/// <param name="workingDirectory">The working directory.</param> | ||
/// <param name="innerException">The exception that is the cause of the current exception.</param> | ||
public ExternalOperationException(string command, string arguments, string workingDirectory, Exception innerException) | ||
: base(null, innerException) | ||
{ | ||
Command = command; | ||
Arguments = arguments; | ||
WorkingDirectory = workingDirectory; | ||
} | ||
|
||
/// <summary> | ||
/// The command that led to the exception. | ||
/// </summary> | ||
public string Command { get; } | ||
|
||
/// <summary> | ||
/// The command arguments. | ||
/// </summary> | ||
public string Arguments { get; } | ||
|
||
/// <summary> | ||
/// The working directory. | ||
/// </summary> | ||
public string WorkingDirectory { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
|
||
#nullable enable | ||
|
||
namespace GitCommands | ||
{ | ||
/// <summary> | ||
/// Represents errors that occur during execution of user-configured operation, e.g. a script. | ||
/// </summary> | ||
public class UserExternalOperationException : ExternalOperationException | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="UserExternalOperationException"/> class with a specified parameters | ||
/// and a reference to the inner exception that is the cause of this exception. | ||
/// </summary> | ||
/// <param name="command">The command that led to the exception.</param> | ||
/// <param name="arguments">The command arguments.</param> | ||
/// <param name="workingDirectory">The working directory.</param> | ||
/// <param name="innerException">The exception that is the cause of the current exception.</param> | ||
public UserExternalOperationException(string command, string arguments, string workingDirectory, Exception innerException) | ||
: base(command, arguments, workingDirectory, innerException) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using System; | ||
using System.Windows.Forms; | ||
using GitCommands; | ||
using GitUI; | ||
using Microsoft.WindowsAPICodePack.Dialogs; | ||
|
||
namespace GitExtensions | ||
{ | ||
public static class BugReporter | ||
{ | ||
public static void Report(Exception ex, bool isTerminating) | ||
{ | ||
if (ex is UserExternalOperationException userExternalException) | ||
{ | ||
// Something happened that was likely caused by the user | ||
ReportUserException(userExternalException, isTerminating); | ||
return; | ||
} | ||
|
||
if (ex is ExternalOperationException externalException) | ||
{ | ||
// Something happened either in the app - can submit the bug to GitHub | ||
ReportAppException(externalException, isTerminating); | ||
return; | ||
} | ||
|
||
// Report any other exceptions | ||
ReportGenericException(ex, isTerminating); | ||
} | ||
|
||
private static void ReportAppException(ExternalOperationException ex, bool isTerminating) | ||
{ | ||
IWin32Window owner = Application.OpenForms.Count > 0 ? Application.OpenForms[0] : null; | ||
|
||
// UserExternalOperationException wraps an actual exception, but be cautious just in case | ||
string message = ex.InnerException?.Message ?? Strings.InstructionOperationFailed; | ||
|
||
using var dialog = new TaskDialog | ||
{ | ||
OwnerWindowHandle = owner?.Handle ?? IntPtr.Zero, | ||
Text = $"{Strings.Command}: {ex.Command.Quote()} {ex.Arguments.QuoteNE()}\r\n{Strings.WorkingDirectory}: {ex.WorkingDirectory.Quote()}\r\n\r\n{Strings.ReportBug}", | ||
InstructionText = message, | ||
Caption = Strings.Error, | ||
Icon = TaskDialogStandardIcon.Error, | ||
Cancelable = true, | ||
}; | ||
var btnReport = new TaskDialogCommandLink("Report", Strings.ButtonReportBug); | ||
btnReport.Click += (s, e) => | ||
{ | ||
dialog.Close(); | ||
ShowNBug(ex); | ||
}; | ||
var btnIgnoreOrClose = new TaskDialogCommandLink("IgnoreOrClose", isTerminating ? Strings.ButtonCloseApp : Strings.ButtonIgnore); | ||
btnIgnoreOrClose.Click += (s, e) => | ||
{ | ||
dialog.Close(); | ||
}; | ||
dialog.Controls.Add(btnReport); | ||
dialog.Controls.Add(btnIgnoreOrClose); | ||
|
||
dialog.Show(); | ||
} | ||
|
||
private static void ReportUserException(UserExternalOperationException ex, bool isTerminating) | ||
{ | ||
IWin32Window owner = Application.OpenForms.Count > 0 ? Application.OpenForms[0] : null; | ||
|
||
// UserExternalOperationException wraps an actual exception, but be cautious just in case | ||
string message = ex.InnerException?.Message ?? Strings.InstructionOperationFailed; | ||
|
||
using var dialog = new TaskDialog | ||
{ | ||
OwnerWindowHandle = owner?.Handle ?? IntPtr.Zero, | ||
Text = $"{Strings.Command}: {ex.Command.Quote()} {ex.Arguments.QuoteNE()}\r\n{Strings.WorkingDirectory}: {ex.WorkingDirectory.Quote()}", | ||
InstructionText = message, | ||
Caption = Strings.CaptionFailedExecute, | ||
Icon = TaskDialogStandardIcon.Error, | ||
Cancelable = true, | ||
}; | ||
var btnIgnoreOrClose = new TaskDialogCommandLink("IgnoreOrClose", isTerminating ? Strings.ButtonCloseApp : Strings.ButtonIgnore); | ||
btnIgnoreOrClose.Click += (s, e) => | ||
{ | ||
dialog.Close(); | ||
}; | ||
dialog.Controls.Add(btnIgnoreOrClose); | ||
|
||
dialog.Show(); | ||
} | ||
|
||
private static void ReportGenericException(Exception ex, bool isTerminating) | ||
{ | ||
IWin32Window owner = Application.OpenForms.Count > 0 ? Application.OpenForms[0] : null; | ||
|
||
// This exception is arbitrary, see if there's additional information | ||
string moreInfo = ex.InnerException?.Message; | ||
if (moreInfo != null) | ||
{ | ||
moreInfo += "\r\n\r\n"; | ||
} | ||
|
||
using var dialog = new TaskDialog | ||
{ | ||
OwnerWindowHandle = owner?.Handle ?? IntPtr.Zero, | ||
Text = $"{moreInfo}{Strings.ReportBug}", | ||
InstructionText = ex.Message, | ||
Caption = Strings.Error, | ||
Icon = TaskDialogStandardIcon.Error, | ||
Cancelable = true, | ||
}; | ||
var btnReport = new TaskDialogCommandLink("Report", Strings.ButtonReportBug); | ||
btnReport.Click += (s, e) => | ||
{ | ||
dialog.Close(); | ||
ShowNBug(ex); | ||
}; | ||
var btnIgnoreOrClose = new TaskDialogCommandLink("IgnoreOrClose", isTerminating ? Strings.ButtonCloseApp : Strings.ButtonIgnore); | ||
btnIgnoreOrClose.Click += (s, e) => | ||
{ | ||
dialog.Close(); | ||
}; | ||
dialog.Controls.Add(btnReport); | ||
dialog.Controls.Add(btnIgnoreOrClose); | ||
|
||
dialog.Show(); | ||
} | ||
|
||
private static void ShowNBug(Exception ex) | ||
{ | ||
var envInfo = UserEnvironmentInformation.GetInformation(); | ||
|
||
using (var form = new GitUI.NBugReports.BugReportForm()) | ||
{ | ||
var result = form.ShowDialog(ex, envInfo); | ||
if (result == DialogResult.Abort) | ||
{ | ||
Environment.Exit(-1); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.