Skip to content

Commit

Permalink
Merge pull request #64900 from raulsntos/dotnet/fix-exceptions
Browse files Browse the repository at this point in the history
Fix various C# exceptions
  • Loading branch information
neikeq authored Aug 28, 2022
2 parents f70e106 + 79f9f59 commit 58f8f3a
Show file tree
Hide file tree
Showing 36 changed files with 122 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public TypeCache(Compilation compilation)
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public T GetResult()
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;
Expand All @@ -39,7 +39,7 @@ public void SetResult(T result)
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class ProjectGenerator
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);

Expand All @@ -37,7 +37,7 @@ public static ProjectRootElement GenGameProject(string name)
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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static void PrintVerbose(string text)
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;

Expand Down Expand Up @@ -111,7 +111,7 @@ private static bool Build(BuildInfo buildInfo)
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;

Expand Down Expand Up @@ -157,7 +157,7 @@ public static async Task<bool> BuildAsync(BuildInfo buildInfo)
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ private void LoadIssuesFromFile(string csvFile)
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];

Expand Down Expand Up @@ -293,15 +293,15 @@ private void ScrollToLastNonEmptyLogLine()
public void RestartBuild()
{
if (!HasBuildExited)
throw new InvalidOperationException("Build already started");
throw new InvalidOperationException("Build already started.");

BuildManager.RestartBuild(this);
}

public void StopBuild()
{
if (!HasBuildExited)
throw new InvalidOperationException("Build is not in progress");
throw new InvalidOperationException("Build is not in progress.");

BuildManager.StopBuild(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private static void AddFallbackFolderToNuGetConfig(string nuGetConfigPath, strin
{
// 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}'.");
}

Expand Down
12 changes: 6 additions & 6 deletions modules/mono/editor/GodotTools/GodotTools/Export/AotBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static void CompileAssembliesForiOS(ExportPlugin exporter, bool isDebug,

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);
}
Expand Down Expand Up @@ -318,7 +318,7 @@ typedef enum {

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);
}
Expand All @@ -336,7 +336,7 @@ typedef enum {

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

Expand Down Expand Up @@ -436,7 +436,7 @@ private static IEnumerable<string> GetAotCompilerArgs(string platform, bool isDe
}
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>
Expand Down Expand Up @@ -533,12 +533,12 @@ string CmdLineArgsToString(IEnumerable<string> args)
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}.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ private void _ExportBeginImpl(string[] features, bool isDebug, string path, int
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";

Expand All @@ -131,7 +131,7 @@ private void _ExportBeginImpl(string[] features, bool isDebug, string path, int
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static string XcodePath
_XcodePath = FindXcode();

if (_XcodePath == null)
throw new Exception("Could not find Xcode");
throw new FileNotFoundException("Could not find Xcode.");
}

return _XcodePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private void ApplyNecessaryChangesToSolution()
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ MonoDevelop.Instance GetMonoDevelopInstance(string solutionPath)
}

default:
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(editorId));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static RiderInfo[] GetAllRiderPaths()
{
return CollectAllRiderPathsLinux();
}
throw new Exception("Unexpected OS.");
throw new InvalidOperationException("Unexpected OS.");
}
catch (Exception e)
{
Expand Down Expand Up @@ -216,7 +216,7 @@ private static string GetRelativePathToBuildTxt()
return "../../build.txt";
if (OS.IsMacOS)
return "Contents/Resources/build.txt";
throw new Exception("Unknown OS.");
throw new InvalidOperationException("Unknown OS.");
}

[SupportedOSPlatform("windows")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public static string[] CodeCompletionRequest(CodeCompletionRequest.CompletionKin
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);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/mono/editor/GodotTools/GodotTools/Utils/OS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public static void RunProcess(string command, IEnumerable<string> arguments)
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();
Expand Down
2 changes: 1 addition & 1 deletion modules/mono/glue/GodotSharp/GodotPlugins/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private static unsafe IntPtr LoadToolsAssembly(char* nAssemblyPath,
string assemblyPath = new(nAssemblyPath);

if (_editorApiAssembly == null)
throw new InvalidOperationException("The Godot editor API assembly is not loaded");
throw new InvalidOperationException("The Godot editor API assembly is not loaded.");

var (assembly, _) = LoadPlugin(assemblyPath);

Expand Down
3 changes: 3 additions & 0 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/AABB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ public real_t GetArea()
/// Gets the position of one of the 8 endpoints of the <see cref="AABB"/>.
/// </summary>
/// <param name="idx">Which endpoint to get.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="idx"/> is less than 0 or greater than 7.
/// </exception>
/// <returns>An endpoint of the <see cref="AABB"/>.</returns>
public Vector3 GetEndpoint(int idx)
{
Expand Down
2 changes: 1 addition & 1 deletion modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private static unsafe void ValidateVariantConversionCallbacks()
if (_convertToVariantCallback == null || _convertToManagedCallback == null)
{
throw new InvalidOperationException(
$"The array element type is not supported for conversion to Variant: '{typeof(T).FullName}'");
$"The array element type is not supported for conversion to Variant: '{typeof(T).FullName}'.");
}
}

Expand Down
11 changes: 7 additions & 4 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Basis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ public Vector3 Scale
/// Access whole columns in the form of <see cref="Vector3"/>.
/// </summary>
/// <param name="column">Which column vector.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="column"/> is not 0, 1, 2 or 3.
/// </exception>
/// <value>The basis column.</value>
public Vector3 this[int column]
{
Expand Down Expand Up @@ -366,8 +369,8 @@ public Quaternion GetRotationQuaternion()
/// but are more efficient for some internal calculations.
/// </summary>
/// <param name="index">Which row.</param>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when the <paramref name="index"/> is not 0, 1 or 2.
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is not 0, 1 or 2.
/// </exception>
/// <returns>One of <c>Row0</c>, <c>Row1</c>, or <c>Row2</c>.</returns>
public Vector3 GetRow(int index)
Expand All @@ -391,8 +394,8 @@ public Vector3 GetRow(int index)
/// </summary>
/// <param name="index">Which row.</param>
/// <param name="value">The vector to set the row to.</param>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when the <paramref name="index"/> is not 0, 1 or 2.
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is not 0, 1 or 2.
/// </exception>
public void SetRow(int index, Vector3 value)
{
Expand Down
2 changes: 1 addition & 1 deletion modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public Color(string code, float alpha)
/// </summary>
/// <param name="rgba">A string for the HTML hexadecimal representation of this color.</param>
/// <exception name="ArgumentOutOfRangeException">
/// Thrown when the given <paramref name="rgba"/> color code is invalid.
/// <paramref name="rgba"/> color code is invalid.
/// </exception>
private static Color FromHTML(string rgba)
{
Expand Down
8 changes: 4 additions & 4 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void Add(Variant key, Variant value)
var self = (godot_dictionary)NativeValue;

if (NativeFuncs.godotsharp_dictionary_contains_key(ref self, variantKey).ToBool())
throw new ArgumentException("An element with the same key already exists", nameof(key));
throw new ArgumentException("An element with the same key already exists.", nameof(key));

godot_variant variantValue = (godot_variant)value.NativeVar;
NativeFuncs.godotsharp_dictionary_add(ref self, variantKey, variantValue);
Expand Down Expand Up @@ -381,13 +381,13 @@ private static unsafe void ValidateVariantConversionCallbacks()
if (_convertKeyToVariantCallback == null || _convertKeyToManagedCallback == null)
{
throw new InvalidOperationException(
$"The dictionary key type is not supported for conversion to Variant: '{typeof(TKey).FullName}'");
$"The dictionary key type is not supported for conversion to Variant: '{typeof(TKey).FullName}'.");
}

if (_convertValueToVariantCallback == null || _convertValueToManagedCallback == null)
{
throw new InvalidOperationException(
$"The dictionary value type is not supported for conversion to Variant: '{typeof(TValue).FullName}'");
$"The dictionary value type is not supported for conversion to Variant: '{typeof(TValue).FullName}'.");
}
}

Expand Down Expand Up @@ -556,7 +556,7 @@ public unsafe void Add(TKey key, TValue value)
var self = (godot_dictionary)_underlyingDict.NativeValue;

if (NativeFuncs.godotsharp_dictionary_contains_key(ref self, variantKey).ToBool())
throw new ArgumentException("An element with the same key already exists", nameof(key));
throw new ArgumentException("An element with the same key already exists.", nameof(key));

using var variantValue = _convertValueToVariantCallback(value);
NativeFuncs.godotsharp_dictionary_add(ref self, variantKey, variantValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public static WeakReference<IDisposable> RegisterDisposable(IDisposable disposab
public static void UnregisterGodotObject(Object godotObject, WeakReference<Object> weakReferenceToSelf)
{
if (!GodotObjectInstances.TryRemove(weakReferenceToSelf, out _))
throw new ArgumentException("Godot Object not registered", nameof(weakReferenceToSelf));
throw new ArgumentException("Godot Object not registered.", nameof(weakReferenceToSelf));
}

public static void UnregisterDisposable(WeakReference<IDisposable> weakReference)
{
if (!OtherInstances.TryRemove(weakReference, out _))
throw new ArgumentException("Disposable not registered", nameof(weakReference));
throw new ArgumentException("Disposable not registered.", nameof(weakReference));
}
}
}
Loading

0 comments on commit 58f8f3a

Please sign in to comment.