Skip to content

Commit

Permalink
Misc: Don't treat exceptions as fatal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol committed Apr 24, 2019
1 parent 644b0f8 commit ce23e16
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 67 deletions.
5 changes: 4 additions & 1 deletion osu!StreamCompanion/Code/Windows/ErrorFrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ protected override bool ShowWithoutActivation
get { return true; }
}

public Error(String Message)
public Error(string Message, string exitText)
{
InitializeComponent();
this.textBox1.Text = Message;

if(!string.IsNullOrEmpty(exitText))
this.label_exitText.Text = exitText;
}

private void button_message_Click(object sender, EventArgs e)
Expand Down
20 changes: 10 additions & 10 deletions osu!StreamCompanion/Code/Windows/ErrorFrm.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

148 changes: 92 additions & 56 deletions osu!StreamCompanion/Program.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
using osu_StreamCompanion.Code.Core;
using osu_StreamCompanion.Code.Core.Loggers;
using osu_StreamCompanion.Code.Helpers;
using osu_StreamCompanion.Code.Windows;
using SharpRaven;
using SharpRaven.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading;
using System.Windows.Forms;
using osu_StreamCompanion.Code.Core;
using osu_StreamCompanion.Code.Core.Loggers;
using osu_StreamCompanion.Code.Helpers;
using osu_StreamCompanion.Code.Windows;
using SharpRaven;
using SharpRaven.Data;

namespace osu_StreamCompanion
{
static class Program
{
public static string ScVersion ="v190307.19";
public static string ScVersion ="v190424.19";
private static Initializer _initializer;
private const bool AllowMultiInstance = false;

/// <summary>
/// The main entry point for the application.
/// </summary>
Expand All @@ -27,52 +30,58 @@ static void Main(string[] args)
{
string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
string mutexId = string.Format("Global\\{{{0}}}", appGuid);
using (var mutex = new Mutex(false, mutexId))
{

var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
mutex.SetAccessControl(securitySettings);

// edited by acidzombie24
var hasHandle = false;
try
if (AllowMultiInstance)
Run();
else
using (var mutex = new Mutex(false, mutexId))
{

var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
mutex.SetAccessControl(securitySettings);

var hasHandle = false;
try
{
hasHandle = mutex.WaitOne(2000, false);
if (hasHandle == false)
try
{
hasHandle = mutex.WaitOne(2000, false);
if (hasHandle == false)
{
MessageBox.Show("osu!StreamCompanion is already running.", "Error");
return;
}
}
catch (AbandonedMutexException)
{
MessageBox.Show("osu!StreamCompanion is already running.", "Error");
return;
hasHandle = true;
}

Run();

}
catch (AbandonedMutexException)
finally
{
hasHandle = true;
if (hasHandle)
mutex.ReleaseMutex();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException += Application_ThreadException;
_initializer = new Initializer();
_initializer.Start();
Application.Run(_initializer);


}
finally
{
if (hasHandle)
mutex.ReleaseMutex();
}
}


}

private static void Run()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException += Application_ThreadException;
_initializer = new Initializer();
_initializer.Start();
Application.Run(_initializer);
}

private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
HandleException(e.Exception);
Expand Down Expand Up @@ -129,35 +138,62 @@ static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEv
HandleException(ex);
}
}
private static string errorNotification = @"There was unhandled problem with a program and it needs to close." + Environment.NewLine + "Error report was sent to Piotrekol";

private static (bool ForceQuit, bool SendReport, string Message) GetErrorData()
{
if (Exceptions.Count <= 4)
return (false, true, $"{errorNotificationFormat} {willAttemptToRun} {messageWasSent}");

return (false, false, $"{errorNotificationFormat} {willAttemptToRun} {messageWasNotSent}");
}

private static string errorNotificationFormat = @"There was unhandled problem with a program";
private static string needToExit = @"and it needs to exit.";
private static string willAttemptToRun = @"but it will attempt to run.";
private static string messageWasSent = "Error report was sent to Piotrekol.";
private static string messageWasNotSent = "Any further errors will !not! be sent to Piotrekol until StreamCompanion restarts.";

private static List<Exception> Exceptions { get; set; }
public static void HandleException(Exception ex)
{
bool shouldQuit = true;
try
{
ex.Data["ExceptionCount"] = Exceptions.Count;
Exceptions.Add(ex);

ex.Data.Add("netFramework", GetDotNetVersion.Get45PlusFromRegistry());
#if !DEBUG
var ravenClient = SentryLogger.RavenClient;
ravenClient.Release = ScVersion;
var sentryEvent = new SentryEvent(ex);
ravenClient.Capture(sentryEvent);

var errorConsensus = GetErrorData();
shouldQuit = errorConsensus.ForceQuit;

#if DEBUG
if (errorConsensus.SendReport)
{
var ravenClient = SentryLogger.RavenClient;
ravenClient.Release = ScVersion;
var sentryEvent = new SentryEvent(ex);
ravenClient.Capture(sentryEvent);
}
#endif
MessageBox.Show(errorNotification, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
var form = new Error(ex.Message + Environment.NewLine + ex.StackTrace);
MessageBox.Show(errorConsensus.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
var form = new Error(ex.Message + Environment.NewLine + ex.StackTrace, !errorConsensus.ForceQuit ? "StreamCompanion will attempt to run" : null);
form.ShowDialog();
}
finally
{
try
{
SafeQuit();
}
catch
if (shouldQuit)
{
_initializer.ExitThread();
try
{
SafeQuit();
}
catch
{
_initializer.ExitThread();
}
}
}
}

}
}

0 comments on commit ce23e16

Please sign in to comment.