Fix various C# exceptions

- Replace `IndexOutOfRangeException` with `ArgumentOutOfRangeException`
- Replace `Exception` with a more specific exception
- Add the parameter name to argument exception
- Update documentation for methods that throw exceptions
- Use `StringBuilder` to build exception messages
- Ensure exception messages end with a period
This commit is contained in:
Raul Santos 2022-08-24 13:54:47 +02:00
parent 9876382df8
commit 79f9f59a87
No known key found for this signature in database
GPG key ID: B532473AE3A803E4
36 changed files with 122 additions and 94 deletions

View file

@ -16,7 +16,7 @@ namespace Godot.SourceGenerators
INamedTypeSymbol GetTypeByMetadataNameOrThrow(string fullyQualifiedMetadataName)
{
return compilation.GetTypeByMetadataName(fullyQualifiedMetadataName) ??
throw new InvalidOperationException("Type not found: " + fullyQualifiedMetadataName);
throw new InvalidOperationException($"Type not found: '{fullyQualifiedMetadataName}'.");
}
GodotObjectType = GetTypeByMetadataNameOrThrow("Godot.Object");

View file

@ -21,14 +21,14 @@ namespace GodotTools.IdeMessaging.Utils
public void OnCompleted(Action continuation)
{
if (this.continuation != null)
throw new InvalidOperationException("This awaiter has already been listened");
throw new InvalidOperationException("This awaiter already has a continuation.");
this.continuation = continuation;
}
public void SetResult(T result)
{
if (IsCompleted)
throw new InvalidOperationException("This awaiter is already completed");
throw new InvalidOperationException("This awaiter is already completed.");
IsCompleted = true;
this.result = result;
@ -39,7 +39,7 @@ namespace GodotTools.IdeMessaging.Utils
public void SetException(Exception exception)
{
if (IsCompleted)
throw new InvalidOperationException("This awaiter is already completed");
throw new InvalidOperationException("This awaiter is already completed.");
IsCompleted = true;
this.exception = exception;

View file

@ -15,7 +15,7 @@ namespace GodotTools.ProjectEditor
public static ProjectRootElement GenGameProject(string name)
{
if (name.Length == 0)
throw new ArgumentException("Project name is empty", nameof(name));
throw new ArgumentException("Project name is empty.", nameof(name));
var root = ProjectRootElement.Create(NewProjectFileOptions.None);
@ -37,7 +37,7 @@ namespace GodotTools.ProjectEditor
public static string GenAndSaveGameProject(string dir, string name)
{
if (name.Length == 0)
throw new ArgumentException("Project name is empty", nameof(name));
throw new ArgumentException("Project name is empty.", nameof(name));
string path = Path.Combine(dir, name + ".csproj");

View file

@ -62,7 +62,7 @@ namespace GodotTools.Build
private static bool Build(BuildInfo buildInfo)
{
if (_buildInProgress != null)
throw new InvalidOperationException("A build is already in progress");
throw new InvalidOperationException("A build is already in progress.");
_buildInProgress = buildInfo;
@ -111,7 +111,7 @@ namespace GodotTools.Build
public static async Task<bool> BuildAsync(BuildInfo buildInfo)
{
if (_buildInProgress != null)
throw new InvalidOperationException("A build is already in progress");
throw new InvalidOperationException("A build is already in progress.");
_buildInProgress = buildInfo;
@ -157,7 +157,7 @@ namespace GodotTools.Build
private static bool Publish(BuildInfo buildInfo)
{
if (_buildInProgress != null)
throw new InvalidOperationException("A build is already in progress");
throw new InvalidOperationException("A build is already in progress.");
_buildInProgress = buildInfo;

View file

@ -120,13 +120,13 @@ namespace GodotTools.Build
private void IssueActivated(int idx)
{
if (idx < 0 || idx >= _issuesList.ItemCount)
throw new IndexOutOfRangeException("Item list index out of range");
throw new ArgumentOutOfRangeException(nameof(idx), "Item list index out of range.");
// Get correct issue idx from issue list
int issueIndex = (int)_issuesList.GetItemMetadata(idx);
if (issueIndex < 0 || issueIndex >= _issues.Count)
throw new IndexOutOfRangeException("Issue index out of range");
throw new InvalidOperationException("Issue index out of range.");
BuildIssue issue = _issues[issueIndex];
@ -293,7 +293,7 @@ namespace GodotTools.Build
public void RestartBuild()
{
if (!HasBuildExited)
throw new InvalidOperationException("Build already started");
throw new InvalidOperationException("Build already started.");
BuildManager.RestartBuild(this);
}
@ -301,7 +301,7 @@ namespace GodotTools.Build
public void StopBuild()
{
if (!HasBuildExited)
throw new InvalidOperationException("Build is not in progress");
throw new InvalidOperationException("Build is not in progress.");
BuildManager.StopBuild(this);
}

View file

@ -49,7 +49,7 @@ namespace GodotTools.Build
{
// Check that the root node is the expected one
if (rootNode.Name != nuGetConfigRootName)
throw new Exception("Invalid root Xml node for NuGet.Config. " +
throw new FormatException("Invalid root Xml node for NuGet.Config. " +
$"Expected '{nuGetConfigRootName}' got '{rootNode.Name}'.");
}

View file

@ -212,7 +212,7 @@ namespace GodotTools.Export
int clangExitCode = OS.ExecuteCommand(XcodeHelper.FindXcodeTool("clang"), clangArgs);
if (clangExitCode != 0)
throw new Exception($"Command 'clang' exited with code: {clangExitCode}");
throw new InvalidOperationException($"Command 'clang' exited with code: {clangExitCode}.");
objFilePathsForiOSArch[arch].Add(objFilePath);
}
@ -318,7 +318,7 @@ MONO_AOT_MODE_LAST = 1000,
int arExitCode = OS.ExecuteCommand(XcodeHelper.FindXcodeTool("ar"), arArgs);
if (arExitCode != 0)
throw new Exception($"Command 'ar' exited with code: {arExitCode}");
throw new InvalidOperationException($"Command 'ar' exited with code: {arExitCode}.");
arFilePathsForAllArchs.Add(arOutputFilePath);
}
@ -336,7 +336,7 @@ MONO_AOT_MODE_LAST = 1000,
int lipoExitCode = OS.ExecuteCommand(XcodeHelper.FindXcodeTool("lipo"), lipoArgs);
if (lipoExitCode != 0)
throw new Exception($"Command 'lipo' exited with code: {lipoExitCode}");
throw new InvalidOperationException($"Command 'lipo' exited with code: {lipoExitCode}.");
// TODO: Add the AOT lib and interpreter libs as device only to suppress warnings when targeting the simulator
@ -436,7 +436,7 @@ MONO_AOT_MODE_LAST = 1000,
}
else if (!Directory.Exists(androidToolchain))
{
throw new FileNotFoundException("Android toolchain not found: " + androidToolchain);
throw new FileNotFoundException($"Android toolchain not found: '{androidToolchain}'.");
}
var androidToolPrefixes = new Dictionary<string, string>
@ -533,12 +533,12 @@ MONO_AOT_MODE_LAST = 1000,
Console.WriteLine($"Running: \"{process.StartInfo.FileName}\" {process.StartInfo.Arguments}");
if (!process.Start())
throw new Exception("Failed to start process for Mono AOT compiler");
throw new InvalidOperationException("Failed to start process for Mono AOT compiler.");
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception($"Mono AOT compiler exited with code: {process.ExitCode}");
throw new InvalidOperationException($"Mono AOT compiler exited with code: {process.ExitCode}.");
}
}

View file

@ -98,16 +98,16 @@ namespace GodotTools.Export
return;
if (!DeterminePlatformFromFeatures(features, out string platform))
throw new NotSupportedException("Target platform not supported");
throw new NotSupportedException("Target platform not supported.");
if (!new[] { OS.Platforms.Windows, OS.Platforms.LinuxBSD, OS.Platforms.MacOS }
.Contains(platform))
{
throw new NotImplementedException("Target platform not yet implemented");
throw new NotImplementedException("Target platform not yet implemented.");
}
string outputDir = new FileInfo(path).Directory?.FullName ??
throw new FileNotFoundException("Output base directory not found");
throw new FileNotFoundException("Output base directory not found.");
string buildConfig = isDebug ? "ExportDebug" : "ExportRelease";
@ -131,7 +131,7 @@ namespace GodotTools.Export
if (!BuildManager.PublishProjectBlocking(buildConfig, platform,
runtimeIdentifier, publishOutputTempDir))
{
throw new Exception("Failed to build project");
throw new InvalidOperationException("Failed to build project.");
}
string soExt = ridOS switch

View file

@ -16,7 +16,7 @@ namespace GodotTools.Export
_XcodePath = FindXcode();
if (_XcodePath == null)
throw new Exception("Could not find Xcode");
throw new FileNotFoundException("Could not find Xcode.");
}
return _XcodePath;

View file

@ -342,7 +342,7 @@ namespace GodotTools
DotNetSolution.MigrateFromOldConfigNames(GodotSharpDirs.ProjectSlnPath);
var msbuildProject = ProjectUtils.Open(GodotSharpDirs.ProjectCsProjPath)
?? throw new Exception("Cannot open C# project");
?? throw new InvalidOperationException("Cannot open C# project.");
// NOTE: The order in which changes are made to the project is important

View file

@ -153,7 +153,7 @@ namespace GodotTools.Ides
}
default:
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(editorId));
}
}

View file

@ -42,7 +42,7 @@ namespace GodotTools.Ides.Rider
{
return CollectAllRiderPathsLinux();
}
throw new Exception("Unexpected OS.");
throw new InvalidOperationException("Unexpected OS.");
}
catch (Exception e)
{
@ -216,7 +216,7 @@ namespace GodotTools.Ides.Rider
return "../../build.txt";
if (OS.IsMacOS)
return "Contents/Resources/build.txt";
throw new Exception("Unknown OS.");
throw new InvalidOperationException("Unknown OS.");
}
[SupportedOSPlatform("windows")]

View file

@ -74,11 +74,11 @@ namespace GodotTools.Internals
internal static unsafe void Initialize(IntPtr unmanagedCallbacks, int unmanagedCallbacksSize)
{
if (initialized)
throw new InvalidOperationException("Already initialized");
throw new InvalidOperationException("Already initialized.");
initialized = true;
if (unmanagedCallbacksSize != sizeof(InternalUnmanagedCallbacks))
throw new ArgumentException("Unmanaged callbacks size mismatch");
throw new ArgumentException("Unmanaged callbacks size mismatch.", nameof(unmanagedCallbacksSize));
_unmanagedCallbacks = Unsafe.AsRef<InternalUnmanagedCallbacks>((void*)unmanagedCallbacks);
}

View file

@ -257,7 +257,7 @@ namespace GodotTools.Utils
using Process process = Process.Start(startInfo);
if (process == null)
throw new Exception("No process was started");
throw new InvalidOperationException("No process was started.");
process.BeginOutputReadLine();
process.BeginErrorReadLine();