Remove generic exceptions. (#2913)

* Replace instances of Exception with better ones in reflection utils.

* Replace instances of Exception with better ones in the websocket project.

* Finish replacing generic exceptions.

* Tiny tweak to reflection utils for consistency with the .NET library.
This commit is contained in:
Nora
2024-05-11 22:23:08 +02:00
committed by GitHub
parent 83fdc8917e
commit 54d2fe5773
10 changed files with 22 additions and 18 deletions

View File

@@ -263,7 +263,7 @@ namespace Discord
var result = await client.PostAsync($"{ApiUrl}/overrides/{id}/dependency", new StringContent($"{{ \"info\": \"{name}\"}}", Encoding.UTF8, "application/json"));
if (!result.IsSuccessStatusCode)
throw new Exception("Failed to get dependency");
throw new HttpRequestException("Failed to get dependency");
using (var ms = new MemoryStream())
{

View File

@@ -37,7 +37,7 @@ namespace Discord.Commands
}
catch (Exception ex)
{
throw new Exception($"Failed to create \"{ownerType.FullName}\".", ex);
throw new TargetInvocationException($"Failed to create \"{ownerType.FullName}\".", ex);
}
}
@@ -45,7 +45,7 @@ namespace Discord.Commands
{
var constructors = ownerType.DeclaredConstructors.Where(x => !x.IsStatic).ToArray();
if (constructors.Length == 0)
throw new InvalidOperationException($"No constructor found for \"{ownerType.FullName}\".");
throw new MissingMethodException($"No constructor found for \"{ownerType.FullName}\".");
else if (constructors.Length > 1)
throw new InvalidOperationException($"Multiple constructors found for \"{ownerType.FullName}\".");
return constructors[0];

View File

@@ -40,14 +40,14 @@ namespace Discord.Interactions
}
catch (Exception ex)
{
throw new Exception($"Failed to create \"{ownerType.FullName}\".", ex);
throw new TargetInvocationException($"Failed to create \"{ownerType.FullName}\".", ex);
}
}
private static ConstructorInfo GetConstructor(TypeInfo ownerType)
{
var constructors = ownerType.DeclaredConstructors.Where(x => !x.IsStatic).ToArray();
if (constructors.Length == 0)
throw new InvalidOperationException($"No constructor found for \"{ownerType.FullName}\".");
throw new MissingMethodException($"No constructor found for \"{ownerType.FullName}\".");
else if (constructors.Length > 1)
throw new InvalidOperationException($"Multiple constructors found for \"{ownerType.FullName}\".");
return constructors[0];

View File

@@ -11,7 +11,7 @@ namespace Discord.Rest
public static byte[] HexToByteArray(string hex)
{
if (hex.Length % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");
throw new ArgumentException("The binary key cannot have an odd number of digits");
byte[] arr = new byte[hex.Length >> 1];

View File

@@ -506,7 +506,7 @@ namespace Discord.Audio
if (_heartbeatTimes.Count != 0 && (now - _lastMessageTime) > intervalMillis &&
ConnectionState == ConnectionState.Connected)
{
_connection.Error(new Exception("Server missed last heartbeat"));
_connection.Error(new WebSocketException(WebSocketError.InvalidState, "Server missed last heartbeat"));
return;
}

View File

@@ -1,4 +1,5 @@
using System;
using System.IO;
namespace Discord.Audio
{
@@ -36,12 +37,12 @@ namespace Discord.Audio
protected static void CheckError(int result)
{
if (result < 0)
throw new Exception($"Opus Error: {(OpusError)result}");
throw new InvalidDataException($"Opus Error: {(OpusError)result}");
}
protected static void CheckError(OpusError error)
{
if ((int)error < 0)
throw new Exception($"Opus Error: {error}");
throw new InvalidDataException($"Opus Error: {error}");
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace Discord.Audio
{
@@ -17,7 +18,7 @@ namespace Discord.Audio
{
int error = SecretBoxEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret);
if (error != 0)
throw new Exception($"Sodium Error: {error}");
throw new SecurityException($"Sodium Error: {error}");
return inputLength + 16;
}
}
@@ -28,7 +29,7 @@ namespace Discord.Audio
{
int error = SecretBoxOpenEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret);
if (error != 0)
throw new Exception($"Sodium Error: {error}");
throw new SecurityException($"Sodium Error: {error}");
return inputLength - 16;
}
}

View File

@@ -1,6 +1,7 @@
using Discord.Logging;
using Discord.Net;
using System;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
@@ -43,15 +44,16 @@ namespace Discord
{
var ex2 = ex as WebSocketClosedException;
if (ex2?.CloseCode == 4006)
CriticalError(new Exception("WebSocket session expired", ex));
CriticalError(new WebSocketException(WebSocketError.ConnectionClosedPrematurely, "WebSocket session expired", ex));
else if (ex2?.CloseCode == 4014)
CriticalError(new Exception("WebSocket connection was closed", ex));
CriticalError(new WebSocketException(WebSocketError.ConnectionClosedPrematurely, "WebSocket connection was closed", ex));
else
Error(new Exception("WebSocket connection was closed", ex));
Error(new WebSocketException(WebSocketError.ConnectionClosedPrematurely, "WebSocket connection was closed", ex));
}
else
Error(new Exception("WebSocket connection was closed"));
return Task.Delay(0);
Error(new WebSocketException(WebSocketError.ConnectionClosedPrematurely, "WebSocket connection was closed"));
return Task.CompletedTask;
});
}

View File

@@ -267,7 +267,7 @@ namespace Discord.Net.WebSockets
}
catch (Win32Exception ex) when (ex.HResult == HR_TIMEOUT)
{
var _ = OnClosed(new Exception("Connection timed out.", ex));
var _ = OnClosed(new WebSocketException(WebSocketError.ConnectionClosedPrematurely, "Connection timed out.", ex));
}
catch (OperationCanceledException) { }
catch (Exception ex)

View File

@@ -16,7 +16,7 @@ namespace Discord
{
var token = Environment.GetEnvironmentVariable("DNET_TEST_TOKEN", EnvironmentVariableTarget.Process);
if (string.IsNullOrWhiteSpace(token))
throw new Exception("The DNET_TEST_TOKEN environment variable was not provided.");
throw new ArgumentException("The DNET_TEST_TOKEN environment variable was not provided.");
Client = new DiscordRestClient(new DiscordRestConfig()
{
LogLevel = LogSeverity.Debug,