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 1 commit
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
Prev Previous commit
Next Next commit
Added some more comments and finished unit testing, just need to play…
…test and then it's ready for review
  • Loading branch information
OhmV-IR committed Feb 24, 2024
commit a23a8edeaf5c8a02a3a6522db32806251221ac3f
138 changes: 128 additions & 10 deletions Nitrox.Test/Model/StatusCodeTests.cs
Original file line number Diff line number Diff line change
@@ -1,35 +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)
{
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)
{
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 ThrowStatusCodesNonFatal()
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 < Enum.GetNames(typeof(StatusCode)).Length; i++)
for (int i = 0; i < statusCodeNames.Length; i++)
{
DisplayStatusCode((StatusCode)i, false, "Testing exception");
DisplayStatusCode((StatusCode)Enum.Parse(typeof(StatusCode), statusCodeNames[i]), false, statusCodeNames[i] + " Testing exception");
}
}
[TestMethod]
public void ThrowStatusCodesFatal() // Should show up as "aborted" after running it in VS
public void ThrowStatusCodesFatalDisplay()
{
// Display one of every status code once fatally, should only display the first one
for (int i = 0; i < Enum.GetNames(typeof(StatusCode)).Length; i++)
// 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"))
{
DisplayStatusCode((StatusCode)i, true, "Testing exception");
throw new Exception("Test failed: Program continued to run after a fatal StatusCode call");
}
throw new Exception("Test failed: Program continued to run after a fatal StatusCode call");

}
[TestMethod]
public void ThrowStatusCodesLongException()
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
52 changes: 46 additions & 6 deletions NitroxModel/DisplayStatusCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ namespace NitroxModel
{
public class DisplayStatusCodes
{
// List all possible status codes, might be a better way to do something repetive like this(could add more descriptive names too)
// Counter variables to check if the same code is happening over and over again to mark it as fatal
private static int repeatCodeCountDisplay = 0;
private static StatusCode lastCodeDisplay = StatusCode.SUCCESS;
private static int repeatCodeCountPrint = 0;
private static StatusCode lastCodePrint = StatusCode.SUCCESS;
// All possible status codes, uses HTTP error codes
public enum StatusCode
{
SUCCESS = 200,
Expand All @@ -31,8 +36,25 @@ public enum StatusCode
LOCK_ERR = 503,
INVALID_FUNCTION_CALL = 405
}
public static bool DisplayStatusCode(StatusCode statusCode, bool fatal, string exception)
public static void DisplayStatusCode(StatusCode statusCode, bool fatal, string exception)
{
// If the statusCode is the same as the last one we displayed, increase the repeated codes counter
if (statusCode == lastCodeDisplay)
{
repeatCodeCountDisplay++;
}
// If the statusCode is not the same, reset the repeated codes counter
else
{
repeatCodeCountDisplay = 0;
}
// If the same code is repeated 3 times in a row(including this code), then the error is fatal
if (repeatCodeCountDisplay == 2)
{
fatal = true;
}
// Set the last statusCode variable for the next time the function runs
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;
Expand All @@ -42,19 +64,37 @@ public static bool DisplayStatusCode(StatusCode statusCode, bool fatal, string e
{
Environment.Exit(1);
}
return true;
// If the error is not fatal, continue running
}

// Print the statusCode to the server console(only for statusCodes that are due to a server-side crash)
public static bool PrintStatusCode(StatusCode statusCode, bool fatal, string exception)
public static void PrintStatusCode(StatusCode statusCode, bool fatal, string exception)
{
// ToString("D") prints the integer value of the statusCode enum
// If the statusCode is the same as the last one we printed, increase the repeated codes counter
if(statusCode == lastCodePrint)
{
repeatCodeCountPrint++;
}
// If the statusCode is different, reset the counter
else
{
repeatCodeCountPrint = 0;
}
// If the same code happens 3 times in a row, then the error is fatal and we will exit nitrox
if(repeatCodeCountPrint == 2)
{
fatal = true;
}
// Set the last code printed variable for the next time the function runs
lastCodePrint = statusCode;
// Log the status code to console along with the exception message
Log.Error(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, exit nitrox
if (fatal)
{
Environment.Exit(1);
}
return true;
// If the error is not fatal, continue running
}
}
}