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

Adding popup messages for errors with status codes alongside the exception message #2116

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
59924de
Initial commit to open PR
OhmV-IR Jan 16, 2024
2bc36cb
Added enum for status codes and function to print them
OhmV-IR Jan 17, 2024
a4afd49
Use integer enum overload
OhmV-IR Jan 18, 2024
3651990
Added project reference and status codes for NitroxConfig.cs
OhmV-IR Jan 26, 2024
cd4392c
Added error messages in a popup for nitroxModel
OhmV-IR Feb 4, 2024
4aaf86d
fix missing import statements so the solution can build
OhmV-IR Feb 4, 2024
6fb3a87
added a better messageBox dialog to display status code errors
OhmV-IR Feb 4, 2024
7488213
moved display status code enum out of nitrox server into new files in…
OhmV-IR Feb 5, 2024
6b23109
MORE FUNCTION CALLS
OhmV-IR Feb 6, 2024
60e673a
added more descriptive names for statusCodes in the files and moved s…
OhmV-IR Feb 12, 2024
e76df2f
Finished adding statusCode calls to NitroxClient
OhmV-IR Feb 12, 2024
388fd96
Check if nitroxLauncher is in a oneDrive folder implementation
OhmV-IR Feb 14, 2024
741099a
Added fatal flag to statusCode function calls
OhmV-IR Feb 20, 2024
ca8a205
Added exception message to StatusCode messageBox
OhmV-IR Feb 20, 2024
051f09a
Added exception messages to PrintStatusCode <- server status codes fu…
OhmV-IR Feb 20, 2024
df46194
Merge pull request #1 from OhmV-IR/inDev
OhmV-IR Feb 20, 2024
2a03491
removed unused isPirate bool in CustomMessagebox.cs
OhmV-IR Feb 20, 2024
9f3e552
Added test fail case for FatalStatusCodes and removed success status …
OhmV-IR Feb 20, 2024
219ef4e
Added text wrapping so that the exception message does not exceed the…
OhmV-IR Feb 21, 2024
cb86f5c
Change statusCode enum to use HTTP status codes and condense redundan…
OhmV-IR Feb 22, 2024
5ca499c
Changed enum names to follow upper snake case convention
OhmV-IR Feb 22, 2024
4a2bf7c
Changed SYNC_FAIL to use a different code than MISSING_FEATURE
OhmV-IR Feb 22, 2024
973015b
Fixed a typo that caused build errors
OhmV-IR Feb 22, 2024
a23a8ed
Added some more comments and finished unit testing, just need to play…
OhmV-IR Feb 24, 2024
f778fe1
Change enum names, use HTTP status codes, unit tests, should be ready…
OhmV-IR Feb 24, 2024
81cce0e
Client disconnects are never recognized as fatal, even if they happen…
OhmV-IR Feb 24, 2024
fc0e7dd
Merge branch 'prod-status-codes' into polishing-status-codes
OhmV-IR Feb 27, 2024
3ba4bbf
Added missing using statement
OhmV-IR Feb 27, 2024
c0b9278
Removed unneeded comments
OhmV-IR Mar 12, 2024
ace18b4
removed a useless comment
OhmV-IR Mar 17, 2024
0b542eb
Removed some usings and fixed using assemblies that had not yet been …
OhmV-IR Mar 18, 2024
1b2cb10
Merge branch 'polishing-status-codes' into master
OhmV-IR Mar 19, 2024
57b8ea2
Merge pull request #6 from OhmV-IR/master
OhmV-IR Mar 19, 2024
0137f03
Removed fatal program closes(its just bad design to end execution so …
OhmV-IR Mar 20, 2024
2baf570
Use Log.InGame for less important codes when in InDev and ignore the …
OhmV-IR Mar 20, 2024
505da81
Fixed creating release builds
OhmV-IR Mar 20, 2024
20b28f7
Changed ingame log string + removed unneeded reference in NitroxLauncher
OhmV-IR Mar 20, 2024
5a1f315
Added more function calls
OhmV-IR Mar 24, 2024
5db1930
Small fixes
OhmV-IR Mar 25, 2024
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
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}
153 changes: 153 additions & 0 deletions Nitrox.Test/Model/StatusCodeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Windows.Forms;
using static NitroxModel.DisplayStatusCodes;
using System.Diagnostics;
namespace NitroxModel;

[TestClass]
public class StatusCodeTests
{
// Vars for counting repeat codes to check if they are infinitely repeating and therefore fatal
private static int repeatCodeCountDisplay = 0;
private static StatusCode lastCodeDisplay = StatusCode.SUCCESS;
private static int repeatCodeCountPrint = 0;
private static StatusCode lastCodePrint = StatusCode.SUCCESS;
public static bool TestFatalDisplayStatusCode(StatusCode statusCode, bool fatal, string exception)
{
// If the statusCode is the same as the last one, increase the repeat code count
if (statusCode == lastCodeDisplay && statusCode != StatusCode.CONNECTION_FAIL_CLIENT)
{
repeatCodeCountDisplay++;
}
else
{
repeatCodeCountDisplay = 0;
}
// If there have been 3 of the same code in a row, including this one, then the error is fatal
if (repeatCodeCountDisplay == 2)
{
fatal = true;
}
// Set the last code variable
lastCodeDisplay = statusCode;
// Display a popup message box using CustomMessageBox.cs which has most of the buttons and strings filled in with a placeholder for the statusCode
CustomMessageBox customMessage = new(statusCode, exception);
customMessage.StartPosition = FormStartPosition.CenterParent;
customMessage.ShowDialog();
// If the error is fatal,
if (fatal)
{
// Normally we would exit nitrox, but we return true for testing
return true;
}
// If the error is not fatal, we would continue running but we return false for testing
return false;
}

// Print the statusCode to the server console(only for statusCodes that are due to a server-side crash)
public static bool TestFatalPrintStatusCode(StatusCode statusCode, bool fatal, string exception)
{
// If the code is the same as the one we last printed, increment the repeated codes counter
if (statusCode == lastCodePrint && statusCode != StatusCode.CONNECTION_FAIL_CLIENT)
{
repeatCodeCountPrint++;
}
// If the code is different, reset the counter
else
{
repeatCodeCountPrint = 0;
}
// If the same code happens 3 times in a row, then the error is fatal
if (repeatCodeCountPrint == 2)
{
fatal = true;
}
// Set the last StatusCode variable
lastCodePrint = statusCode;
// Log the statusCode message to console
Debug.WriteLine(string.Concat("Status code = ", statusCode.ToString("D"), " <- Look up this code on the nitrox website for more information about this error." + "Exception message: " + exception));
// If the error is fatal,
if (fatal)
{
// Normally we would exit nitrox, but we return true for testing
return true;
}
// If the error is not fatal, we would continue running but we return false for testing purposes
return false;
}


[TestMethod]
public void ThrowStatusCodesNonFatalDisplay()
{
var statusCodeNames = Enum.GetNames(typeof(StatusCode));
// Display one of every status code once non-fatally, should display all of them once and show "test passed"
for (int i = 0; i < statusCodeNames.Length; i++)
{
DisplayStatusCode((StatusCode)Enum.Parse(typeof(StatusCode), statusCodeNames[i]), false, statusCodeNames[i] + " Testing exception");
}
}
[TestMethod]
public void ThrowStatusCodesFatalDisplay()
{
// Display one of every status code once fatally, should only display the first one and then exit, showing "Test aborted"
if (!TestFatalDisplayStatusCode(StatusCode.DEAD_PIRATES_TELL_NO_TALES, true, StatusCode.DEAD_PIRATES_TELL_NO_TALES.ToString() + " Testing exception"))
{
throw new Exception("Test failed: Program continued to run after a fatal StatusCode call");
}

}
[TestMethod]
public void ThrowStatusCodesLongExceptionDisplay()
{

// Test if the text will wrap or if the messageBox will expand to accomodate for a long exception message
DisplayStatusCode(StatusCode.FILE_SYSTEM_ERR, false, "This is a testing exception that is super long to test if text will wrap to fit the message box space, or potentially expand the message box as needed. Did you know that you are wasting your time reading this? Like actually there is nothing here you can stop reading, just run the test and you will see this text there. Alright this is probably long enough hopefully it works :) and btw Crabsnake is the best mod.");
}
[TestMethod]
public void InfinitelyThrowSameCodeDisplay()
{ // Test if after throwing the same message 3 times, if it will exit fatally
TestFatalDisplayStatusCode(StatusCode.INVALID_VARIABLE_VAL, false, " Infinite loop testing exception");
TestFatalDisplayStatusCode(StatusCode.INVALID_VARIABLE_VAL, false, " Infinite loop testing exception");
if (!TestFatalDisplayStatusCode(StatusCode.INVALID_VARIABLE_VAL, false, " Infinite loop testing exception"))
{
throw new Exception("Test failed: program did not exit after 3 StatusCode exception calls for the same error");
}
}
[TestMethod]
public void InfinitelyThrowSameCodePrint()
{
TestFatalPrintStatusCode(StatusCode.INVALID_VARIABLE_VAL, false, " Infinite loop testing exception");
TestFatalPrintStatusCode(StatusCode.INVALID_VARIABLE_VAL, false, " Infinite loop testing exception");
if (!TestFatalPrintStatusCode(StatusCode.INVALID_VARIABLE_VAL, false, " Infinite loop testing exception"))
{
throw new Exception("Test failed: program did not exit after 3 StatusCode exception calls for the same error");
}
}
[TestMethod]
public void ThrowStatusCodesNonFatalPrint()
{
// Display one of every status code once non-fatally, should display all of them once and show "test passed"
var statusCodeNames = Enum.GetNames(typeof(StatusCode));
for (int i = 0; i < statusCodeNames.Length; i++)
{
PrintStatusCode((StatusCode)Enum.Parse(typeof(StatusCode), statusCodeNames[i]), false, statusCodeNames[i] + " Testing exception");
}
}
[TestMethod]
public void ThrowStatusCodesFatalPrint()
{
// Display one of every status code once fatally, should only display the first one
if (!TestFatalPrintStatusCode(StatusCode.DEAD_PIRATES_TELL_NO_TALES, true, StatusCode.DEAD_PIRATES_TELL_NO_TALES.ToString() + "Testing exception"))
{
throw new Exception("Test failed: Program continued to run after a fatal StatusCode call");
}
}
[TestMethod]
public void ThrowStatusCodesLongExceptionPrint()
{
// Test if the text will wrap or if the messageBox will expand to accomodate for a long exception message
PrintStatusCode(StatusCode.FILE_SYSTEM_ERR, false, "This is a testing exception that is super long to test if text will wrap to fit the message box space, or potentially expand the message box as needed. Did you know that you are wasting your time reading this? Like actually there is nothing here you can stop reading, just run the test and you will see this text there. Alright this is probably long enough hopefully it works :) and btw Crabsnake is the best mod.");
}
}
4 changes: 4 additions & 0 deletions Nitrox.Test/Nitrox.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<ProjectReference Include="..\NitroxServer\NitroxServer.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Windows.Forms" />
</ItemGroup>

<Target Name="MoveNitroxAssetsToTestOutput" AfterTargets="Build" Condition="'$(OS)' == 'Windows_NT'">
<ItemGroup>
<NitroxSubnauticaAssets Include="..\Nitrox.Assets.Subnautica\**\*." />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;

using System;
using static NitroxModel.DisplayStatusCodes;
namespace NitroxClient.Communication.Exceptions
{
public class ClientConnectionFailedException : Exception
{
public ClientConnectionFailedException(string message) : base(message)
{
DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "Client failed to connect to the server and has been disconnected" + message);
}

public ClientConnectionFailedException(string message, Exception innerException) : base(message, innerException)
{
DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "Client failed to connect to the server and has been disconnected" + message);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System;
using System.Threading.Tasks;
using NitroxClient.Communication.Abstract;
using NitroxModel.Helper;
using NitroxModel.MultiplayerSession;
using NitroxModel.Packets;

using static NitroxModel.DisplayStatusCodes;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
public class AwaitingReservationCredentials : ConnectionNegotiatingState
Expand Down Expand Up @@ -56,7 +56,7 @@ private static void ClientIsConnected(IMultiplayerSessionConnectionContext sessi
{
if (!sessionConnectionContext.Client.IsConnected)
{
throw new InvalidOperationException("The client is not connected.");
DisplayStatusCode(StatusCode.INVALID_PACKET, false, "The client is not connected.");
}
}

Expand All @@ -68,7 +68,7 @@ private static void PlayerSettingsIsNotNull(IMultiplayerSessionConnectionContext
}
catch (ArgumentNullException ex)
{
throw new InvalidOperationException("The context does not contain player settings.", ex);
DisplayStatusCode(StatusCode.INVALID_PACKET, false, "The context does not contain player settings." + ex);
}
}

Expand All @@ -80,7 +80,7 @@ private static void AuthenticationContextIsNotNull(IMultiplayerSessionConnection
}
catch (ArgumentNullException ex)
{
throw new InvalidOperationException("The context does not contain an authentication context.", ex);
DisplayStatusCode(StatusCode.INVALID_PACKET, false, "The context does not contain an authentication context." + ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System;
using System.Threading.Tasks;
using NitroxClient.Communication.Abstract;
using NitroxModel.Helper;
using NitroxModel.MultiplayerSession;
using NitroxModel.Packets.Exceptions;

using static NitroxModel.DisplayStatusCodes;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
public class AwaitingSessionReservation : ConnectionNegotiatingState
Expand Down Expand Up @@ -59,14 +59,15 @@ private static void ReservationIsNotNull(IMultiplayerSessionConnectionContext se
}
catch (ArgumentNullException ex)
{
throw new InvalidOperationException("The context does not have a reservation.", ex);
DisplayStatusCode(StatusCode.INVALID_PACKET, false, "The context does not have a reservation." + ex.ToString());
}
}

private void ReservationPacketIsCorrelated(IMultiplayerSessionConnectionContext sessionConnectionContext)
{
if (!reservationCorrelationId.Equals(sessionConnectionContext.Reservation.CorrelationId))
{
DisplayStatusCode(StatusCode.INVALID_PACKET, false, sessionConnectionContext.Reservation.ToString() + reservationCorrelationId.ToString());
throw new UncorrelatedPacketException(sessionConnectionContext.Reservation, reservationCorrelationId);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using System.Threading.Tasks;
using NitroxClient.Communication.Abstract;

using NitroxModel;
using static NitroxModel.DisplayStatusCodes;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
public abstract class CommunicatingState : IMultiplayerSessionConnectionState
Expand All @@ -14,7 +15,7 @@ public virtual void Disconnect(IMultiplayerSessionConnectionContext sessionConne
{
sessionConnectionContext.ClearSessionState();
sessionConnectionContext.Client.Stop();

DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "Client was disconnected.");
Disconnected newConnectionState = new Disconnected();
sessionConnectionContext.UpdateConnectionState(newConnectionState);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System;
using System.Threading.Tasks;
using NitroxClient.Communication.Abstract;

using static NitroxModel.DisplayStatusCodes;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
public abstract class ConnectionNegotiatedState : CommunicatingState
{
public override Task NegotiateReservationAsync(IMultiplayerSessionConnectionContext sessionConnectionContext)
{
throw new InvalidOperationException("Unable to negotiate a session connection in the current state.");
DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "Unable to negotiate a session connection in the current state.");
throw new Exception();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System;
using NitroxClient.Communication.Abstract;

using static NitroxModel.DisplayStatusCodes;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
public abstract class ConnectionNegotiatingState : CommunicatingState
{
public override void JoinSession(IMultiplayerSessionConnectionContext sessionConnectionContext)
{
throw new InvalidOperationException("Cannot join a session until a reservation has been negotiated with the server.");
DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "Cannot join a session until a reservation has been negotiated with the server.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System;
using System.Threading.Tasks;
using NitroxClient.Communication.Abstract;
using NitroxClient.Communication.Exceptions;
using NitroxModel.Helper;
using NitroxModel.Packets;

using static NitroxModel.DisplayStatusCodes;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
public class Disconnected : IMultiplayerSessionConnectionState
Expand Down Expand Up @@ -32,7 +32,7 @@ private void ValidateState(IMultiplayerSessionConnectionContext sessionConnectio
}
catch (ArgumentNullException ex)
{
throw new InvalidOperationException("The context is missing an IP address.", ex);
DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "The context is missing an IP address." + ex.ToString());
}
}

Expand All @@ -44,7 +44,7 @@ private static void ValidateClient(IMultiplayerSessionConnectionContext sessionC
}
catch (ArgumentNullException ex)
{
throw new InvalidOperationException("The client must be set on the connection context before trying to negotiate a session reservation.", ex);
DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "The client must be set on the connection context before trying to negotiate a session reservation." + ex.ToString());
}
}

Expand All @@ -56,7 +56,7 @@ private static async Task StartClientAsync(string ipAddress, IClient client, int

if (!client.IsConnected)
{
throw new ClientConnectionFailedException("The client failed to connect without providing a reason why.");
DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "The client failed to connect without providing a reason why.");
}
}
}
Expand All @@ -74,12 +74,12 @@ private static void EstablishSessionPolicy(IMultiplayerSessionConnectionContext

public void JoinSession(IMultiplayerSessionConnectionContext sessionConnectionContext)
{
throw new InvalidOperationException("Cannot join a session until a reservation has been negotiated with the server.");
DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "Cannot join a session until a reservation has been negotiated with the server.");
}

public void Disconnect(IMultiplayerSessionConnectionContext sessionConnectionContext)
{
throw new InvalidOperationException("Not connected to a multiplayer server.");
DisplayStatusCode(StatusCode.CONNECTION_FAIL_CLIENT, false, "Not connected to a multiplayer server.");
}
}
}
Loading