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

@ -153,7 +153,7 @@ namespace GodotPlugins
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);

View file

@ -145,6 +145,9 @@ namespace Godot
/// 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)
{

View file

@ -510,7 +510,7 @@ namespace Godot.Collections
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}'.");
}
}

View file

@ -148,6 +148,9 @@ namespace Godot
/// 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]
{
@ -366,8 +369,8 @@ namespace Godot
/// 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)
@ -391,8 +394,8 @@ namespace Godot
/// </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)
{

View file

@ -595,7 +595,7 @@ namespace Godot
/// </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)
{

View file

@ -171,7 +171,7 @@ namespace Godot.Collections
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);
@ -381,13 +381,13 @@ namespace Godot.Collections
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}'.");
}
}
@ -556,7 +556,7 @@ namespace Godot.Collections
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);

View file

@ -83,13 +83,13 @@ namespace Godot
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));
}
}
}

View file

@ -36,7 +36,7 @@ namespace Godot
/// <seealso cref="GetNodeOrNull{T}(NodePath)"/>
/// <param name="path">The path to the node to fetch.</param>
/// <exception cref="InvalidCastException">
/// Thrown when the given the fetched node can't be casted to the given type <typeparamref name="T"/>.
/// The fetched node can't be casted to the given type <typeparamref name="T"/>.
/// </exception>
/// <typeparam name="T">The type to cast to. Should be a descendant of <see cref="Node"/>.</typeparam>
/// <returns>
@ -100,7 +100,7 @@ namespace Godot
/// parameter in <see cref="AddChild(Node, bool, InternalMode)"/>).
/// </param>
/// <exception cref="InvalidCastException">
/// Thrown when the given the fetched node can't be casted to the given type <typeparamref name="T"/>.
/// The fetched node can't be casted to the given type <typeparamref name="T"/>.
/// </exception>
/// <typeparam name="T">The type to cast to. Should be a descendant of <see cref="Node"/>.</typeparam>
/// <returns>
@ -142,7 +142,7 @@ namespace Godot
/// </summary>
/// <seealso cref="GetOwnerOrNull{T}"/>
/// <exception cref="InvalidCastException">
/// Thrown when the given the fetched node can't be casted to the given type <typeparamref name="T"/>.
/// The fetched node can't be casted to the given type <typeparamref name="T"/>.
/// </exception>
/// <typeparam name="T">The type to cast to. Should be a descendant of <see cref="Node"/>.</typeparam>
/// <returns>
@ -176,7 +176,7 @@ namespace Godot
/// </summary>
/// <seealso cref="GetParentOrNull{T}"/>
/// <exception cref="InvalidCastException">
/// Thrown when the given the fetched node can't be casted to the given type <typeparamref name="T"/>.
/// The fetched node can't be casted to the given type <typeparamref name="T"/>.
/// </exception>
/// <typeparam name="T">The type to cast to. Should be a descendant of <see cref="Node"/>.</typeparam>
/// <returns>

View file

@ -11,7 +11,7 @@ namespace Godot
/// </summary>
/// <seealso cref="InstantiateOrNull{T}(GenEditState)"/>
/// <exception cref="InvalidCastException">
/// Thrown when the given the instantiated node can't be casted to the given type <typeparamref name="T"/>.
/// The instantiated node can't be casted to the given type <typeparamref name="T"/>.
/// </exception>
/// <typeparam name="T">The type to cast to. Should be a descendant of <see cref="Node"/>.</typeparam>
/// <returns>The instantiated scene.</returns>

View file

@ -19,7 +19,7 @@ namespace Godot
/// Returns an empty resource if no <see cref="ResourceFormatLoader"/> could handle the file.
/// </summary>
/// <exception cref="InvalidCastException">
/// Thrown when the given the loaded resource can't be casted to the given type <typeparamref name="T"/>.
/// The loaded resource can't be casted to the given type <typeparamref name="T"/>.
/// </exception>
/// <typeparam name="T">The type to cast to. Should be a descendant of <see cref="Resource"/>.</typeparam>
public static T Load<T>(string path, string typeHint = null, CacheMode cacheMode = CacheMode.Reuse) where T : class

View file

@ -22,11 +22,11 @@ namespace Godot.NativeInterop
public static void Initialize(IntPtr unmanagedCallbacks, int unmanagedCallbacksSize)
{
if (initialized)
throw new InvalidOperationException("Already initialized");
throw new InvalidOperationException("Already initialized.");
initialized = true;
if (unmanagedCallbacksSize != sizeof(UnmanagedCallbacks))
throw new ArgumentException("Unmanaged callbacks size mismatch");
throw new ArgumentException("Unmanaged callbacks size mismatch.", nameof(unmanagedCallbacksSize));
_unmanagedCallbacks = Unsafe.AsRef<UnmanagedCallbacks>((void*)unmanagedCallbacks);
}

View file

@ -1,4 +1,5 @@
using System;
using System.Text;
#nullable enable
@ -60,19 +61,22 @@ namespace Godot
{
get
{
string s = base.Message;
if (string.IsNullOrEmpty(s))
StringBuilder sb;
if (string.IsNullOrEmpty(base.Message))
{
s = Arg_NativeConstructorNotFoundException;
sb = new(Arg_NativeConstructorNotFoundException);
}
else
{
sb = new(base.Message);
}
if (!string.IsNullOrEmpty(_nativeClassName))
{
s += " " + string.Format("(Class '{0}')", _nativeClassName);
sb.Append($" (Method '{_nativeClassName}')");
}
return s;
return sb.ToString();
}
}
}
@ -115,19 +119,22 @@ namespace Godot
{
get
{
string s = base.Message;
if (string.IsNullOrEmpty(s))
StringBuilder sb;
if (string.IsNullOrEmpty(base.Message))
{
s = Arg_NativeMethodBindNotFoundException;
sb = new(Arg_NativeMethodBindNotFoundException);
}
else
{
sb = new(base.Message);
}
if (!string.IsNullOrEmpty(_nativeMethodName))
{
s += " " + string.Format("(Method '{0}')", _nativeMethodName);
sb.Append($" (Method '{_nativeMethodName}')");
}
return s;
return sb.ToString();
}
}
}

View file

@ -649,6 +649,9 @@ namespace Godot
/// Access whole columns in the form of <see cref="Vector4"/>.
/// </summary>
/// <param name="column">Which column vector.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="column"/> is not 0, 1, 2 or 3.
/// </exception>
public Vector4 this[int column]
{
get
@ -664,7 +667,7 @@ namespace Godot
case 3:
return w;
default:
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(column));
}
}
set
@ -684,7 +687,7 @@ namespace Godot
w = value;
return;
default:
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(column));
}
}
}
@ -694,6 +697,9 @@ namespace Godot
/// </summary>
/// <param name="column">Which column vector.</param>
/// <param name="row">Which row of the column.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="column"/> or <paramref name="row"/> are not 0, 1, 2 or 3.
/// </exception>
public real_t this[int column, int row]
{
get
@ -709,7 +715,7 @@ namespace Godot
case 3:
return w[row];
default:
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(column));
}
}
set
@ -729,7 +735,7 @@ namespace Godot
w[row] = value;
return;
default:
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(column));
}
}
}

View file

@ -47,6 +47,9 @@ namespace Godot
/// <summary>
/// Access quaternion components using their index.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is not 0, 1, 2 or 3.
/// </exception>
/// <value>
/// <c>[0]</c> is equivalent to <see cref="x"/>,
/// <c>[1]</c> is equivalent to <see cref="y"/>,
@ -170,7 +173,7 @@ namespace Godot
#if DEBUG
if (!IsNormalized())
{
throw new InvalidOperationException("Quaternion is not normalized");
throw new InvalidOperationException("Quaternion is not normalized.");
}
#endif
var basis = new Basis(this);
@ -186,7 +189,7 @@ namespace Godot
#if DEBUG
if (!IsNormalized())
{
throw new InvalidOperationException("Quaternion is not normalized");
throw new InvalidOperationException("Quaternion is not normalized.");
}
#endif
return new Quaternion(-x, -y, -z, w);
@ -224,11 +227,11 @@ namespace Godot
#if DEBUG
if (!IsNormalized())
{
throw new InvalidOperationException("Quaternion is not normalized");
throw new InvalidOperationException("Quaternion is not normalized.");
}
if (!to.IsNormalized())
{
throw new ArgumentException("Argument is not normalized", nameof(to));
throw new ArgumentException("Argument is not normalized.", nameof(to));
}
#endif
@ -388,7 +391,7 @@ namespace Godot
#if DEBUG
if (!axis.IsNormalized())
{
throw new ArgumentException("Argument is not normalized", nameof(axis));
throw new ArgumentException("Argument is not normalized.", nameof(axis));
}
#endif
@ -444,7 +447,7 @@ namespace Godot
#if DEBUG
if (!quaternion.IsNormalized())
{
throw new InvalidOperationException("Quaternion is not normalized");
throw new InvalidOperationException("Quaternion is not normalized.");
}
#endif
var u = new Vector3(quaternion.x, quaternion.y, quaternion.z);

View file

@ -75,6 +75,9 @@ namespace Godot
/// The third column is the <see cref="origin"/> vector.
/// </summary>
/// <param name="column">Which column vector.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="column"/> is not 0, 1 or 2.
/// </exception>
public Vector2 this[int column]
{
get

View file

@ -32,6 +32,9 @@ namespace Godot
/// The fourth column is the <see cref="origin"/> vector.
/// </summary>
/// <param name="column">Which column vector.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="column"/> is not 0, 1, 2 or 3.
/// </exception>
public Vector3 this[int column]
{
get

View file

@ -39,8 +39,8 @@ namespace Godot
/// <summary>
/// Access vector components using their index.
/// </summary>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when the given the <paramref name="index"/> is not 0 or 1.
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is not 0 or 1.
/// </exception>
/// <value>
/// <c>[0]</c> is equivalent to <see cref="x"/>,
@ -479,7 +479,7 @@ namespace Godot
#if DEBUG
if (!normal.IsNormalized())
{
throw new ArgumentException("Argument is not normalized", nameof(normal));
throw new ArgumentException("Argument is not normalized.", nameof(normal));
}
#endif
return (2 * Dot(normal) * normal) - this;

View file

@ -39,8 +39,8 @@ namespace Godot
/// <summary>
/// Access vector components using their index.
/// </summary>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when the given the <paramref name="index"/> is not 0 or 1.
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is not 0 or 1.
/// </exception>
/// <value>
/// <c>[0]</c> is equivalent to <see cref="x"/>,

View file

@ -48,8 +48,8 @@ namespace Godot
/// <summary>
/// Access vector components using their index.
/// </summary>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when the given the <paramref name="index"/> is not 0, 1 or 2.
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is not 0, 1 or 2.
/// </exception>
/// <value>
/// <c>[0]</c> is equivalent to <see cref="x"/>,
@ -497,7 +497,7 @@ namespace Godot
#if DEBUG
if (!normal.IsNormalized())
{
throw new ArgumentException("Argument is not normalized", nameof(normal));
throw new ArgumentException("Argument is not normalized.", nameof(normal));
}
#endif
return (2.0f * Dot(normal) * normal) - this;
@ -515,7 +515,7 @@ namespace Godot
#if DEBUG
if (!axis.IsNormalized())
{
throw new ArgumentException("Argument is not normalized", nameof(axis));
throw new ArgumentException("Argument is not normalized.", nameof(axis));
}
#endif
return new Basis(axis, angle) * this;

View file

@ -48,8 +48,8 @@ namespace Godot
/// <summary>
/// Access vector components using their <paramref name="index"/>.
/// </summary>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when the given the <paramref name="index"/> is not 0, 1 or 2.
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is not 0, 1 or 2.
/// </exception>
/// <value>
/// <c>[0]</c> is equivalent to <see cref="x"/>,

View file

@ -57,8 +57,8 @@ namespace Godot
/// <summary>
/// Access vector components using their index.
/// </summary>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when the given the <paramref name="index"/> is not 0, 1, 2 or 3.
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is not 0, 1, 2 or 3.
/// </exception>
/// <value>
/// <c>[0]</c> is equivalent to <see cref="x"/>,
@ -81,7 +81,7 @@ namespace Godot
case 3:
return w;
default:
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(index));
}
}
set
@ -101,7 +101,7 @@ namespace Godot
w = value;
return;
default:
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(index));
}
}
}

View file

@ -57,8 +57,8 @@ namespace Godot
/// <summary>
/// Access vector components using their <paramref name="index"/>.
/// </summary>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when the given the <paramref name="index"/> is not 0, 1, 2 or 3.
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is not 0, 1, 2 or 3.
/// </exception>
/// <value>
/// <c>[0]</c> is equivalent to <see cref="x"/>,
@ -81,7 +81,7 @@ namespace Godot
case 3:
return w;
default:
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(index));
}
}
set
@ -101,7 +101,7 @@ namespace Godot
w = value;
return;
default:
throw new IndexOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(index));
}
}
}