From 54d2fe5773198bb3b7a48e004f845b39331b7806 Mon Sep 17 00:00:00 2001 From: Nora <51166756+AnalogFeelings@users.noreply.github.com> Date: Sat, 11 May 2024 22:23:08 +0200 Subject: [PATCH] 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. --- .../Discord.Net.BuildOverrides/BuildOverrides.cs | 2 +- .../Utilities/ReflectionUtils.cs | 4 ++-- .../Utilities/ReflectionUtils.cs | 4 ++-- src/Discord.Net.Rest/Utils/HexConverter.cs | 2 +- src/Discord.Net.WebSocket/Audio/AudioClient.cs | 2 +- .../Audio/Opus/OpusConverter.cs | 5 +++-- src/Discord.Net.WebSocket/Audio/Sodium/SecretBox.cs | 5 +++-- src/Discord.Net.WebSocket/ConnectionManager.cs | 12 +++++++----- .../Net/DefaultWebSocketClient.cs | 2 +- .../DiscordRestClientFixture.cs | 2 +- 10 files changed, 22 insertions(+), 18 deletions(-) diff --git a/experiment/Discord.Net.BuildOverrides/BuildOverrides.cs b/experiment/Discord.Net.BuildOverrides/BuildOverrides.cs index 91468980..f13821ae 100644 --- a/experiment/Discord.Net.BuildOverrides/BuildOverrides.cs +++ b/experiment/Discord.Net.BuildOverrides/BuildOverrides.cs @@ -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()) { diff --git a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs index ec17be90..28984448 100644 --- a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs +++ b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs @@ -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]; diff --git a/src/Discord.Net.Interactions/Utilities/ReflectionUtils.cs b/src/Discord.Net.Interactions/Utilities/ReflectionUtils.cs index e642495c..1d14f371 100644 --- a/src/Discord.Net.Interactions/Utilities/ReflectionUtils.cs +++ b/src/Discord.Net.Interactions/Utilities/ReflectionUtils.cs @@ -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]; diff --git a/src/Discord.Net.Rest/Utils/HexConverter.cs b/src/Discord.Net.Rest/Utils/HexConverter.cs index ebd959dc..72e6feaf 100644 --- a/src/Discord.Net.Rest/Utils/HexConverter.cs +++ b/src/Discord.Net.Rest/Utils/HexConverter.cs @@ -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]; diff --git a/src/Discord.Net.WebSocket/Audio/AudioClient.cs b/src/Discord.Net.WebSocket/Audio/AudioClient.cs index 018547ce..40ef631d 100644 --- a/src/Discord.Net.WebSocket/Audio/AudioClient.cs +++ b/src/Discord.Net.WebSocket/Audio/AudioClient.cs @@ -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; } diff --git a/src/Discord.Net.WebSocket/Audio/Opus/OpusConverter.cs b/src/Discord.Net.WebSocket/Audio/Opus/OpusConverter.cs index c8a164fb..deb62dd6 100644 --- a/src/Discord.Net.WebSocket/Audio/Opus/OpusConverter.cs +++ b/src/Discord.Net.WebSocket/Audio/Opus/OpusConverter.cs @@ -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}"); } } } diff --git a/src/Discord.Net.WebSocket/Audio/Sodium/SecretBox.cs b/src/Discord.Net.WebSocket/Audio/Sodium/SecretBox.cs index 5ace823c..0cec5402 100644 --- a/src/Discord.Net.WebSocket/Audio/Sodium/SecretBox.cs +++ b/src/Discord.Net.WebSocket/Audio/Sodium/SecretBox.cs @@ -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; } } diff --git a/src/Discord.Net.WebSocket/ConnectionManager.cs b/src/Discord.Net.WebSocket/ConnectionManager.cs index 53d12203..aaad7b16 100644 --- a/src/Discord.Net.WebSocket/ConnectionManager.cs +++ b/src/Discord.Net.WebSocket/ConnectionManager.cs @@ -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; }); } diff --git a/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs b/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs index bcdbf903..ee41ed62 100644 --- a/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs +++ b/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs @@ -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) diff --git a/test/Discord.Net.Tests.Integration/DiscordRestClientFixture.cs b/test/Discord.Net.Tests.Integration/DiscordRestClientFixture.cs index 810e4587..093a55da 100644 --- a/test/Discord.Net.Tests.Integration/DiscordRestClientFixture.cs +++ b/test/Discord.Net.Tests.Integration/DiscordRestClientFixture.cs @@ -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,