From 35684ccccae475d5af9479a3c8d8572c6eafacf4 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 6 Dec 2022 02:35:37 +0000 Subject: [PATCH] Move exception messages to resource file --- VpSharp/res/ExceptionMessages.Designer.cs | 162 ++++++++++++++++++ VpSharp/res/ExceptionMessages.resx | 54 ++++++ VpSharp/src/Entities/VirtualParadiseAvatar.cs | 10 +- VpSharp/src/Entities/VirtualParadiseUser.cs | 10 +- .../src/VirtualParadiseClient.Extensions.cs | 9 +- VpSharp/src/VirtualParadiseClient.Objects.cs | 12 +- VpSharp/src/VirtualParadiseClient.cs | 26 ++- VpSharp/src/WorldSettingsBuilder.cs | 2 +- 8 files changed, 249 insertions(+), 36 deletions(-) diff --git a/VpSharp/res/ExceptionMessages.Designer.cs b/VpSharp/res/ExceptionMessages.Designer.cs index 56d908d..242b2c9 100644 --- a/VpSharp/res/ExceptionMessages.Designer.cs +++ b/VpSharp/res/ExceptionMessages.Designer.cs @@ -59,6 +59,69 @@ namespace VpSharp { } } + /// + /// Looks up a localized string similar to The target avatar is not in the world.. + /// + internal static string AvatarNotInWorld { + get { + return ResourceManager.GetString("AvatarNotInWorld", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Cannot invite non-existent user.. + /// + internal static string CannotInviteNonExistentUser { + get { + return ResourceManager.GetString("CannotInviteNonExistentUser", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Cannot join non-existent user.. + /// + internal static string CannotJoinNonExistentUser { + get { + return ResourceManager.GetString("CannotJoinNonExistentUser", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Cannot login due to incomplete configuration.. + /// + internal static string CannotLogin_IncompleteConfiguration { + get { + return ResourceManager.GetString("CannotLogin_IncompleteConfiguration", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The specified username and password constitute an invalid login.. + /// + internal static string CannotLogin_InvalidLogin { + get { + return ResourceManager.GetString("CannotLogin_InvalidLogin", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A value in the configuration is too long. (BotName?). + /// + internal static string CannotLogin_StringTooLong { + get { + return ResourceManager.GetString("CannotLogin_StringTooLong", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The login request timed out.. + /// + internal static string CannotLogin_Timeout { + get { + return ResourceManager.GetString("CannotLogin_Timeout", resourceCulture); + } + } + /// /// Looks up a localized string similar to The operation cannot be performed on the client's current avatar.. /// @@ -68,6 +131,51 @@ namespace VpSharp { } } + /// + /// Looks up a localized string similar to Connection to the server timed out.. + /// + internal static string ConnectionTimedOut { + get { + return ResourceManager.GetString("ConnectionTimedOut", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A connection to a universe server is required for this operation.. + /// + internal static string ConnectionToUniverseServerRequired { + get { + return ResourceManager.GetString("ConnectionToUniverseServerRequired", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A connection to a world server is required for this operation.. + /// + internal static string ConnectionToWorldServerRequired { + get { + return ResourceManager.GetString("ConnectionToWorldServerRequired", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to No extension with the type {0} is added to this client.. + /// + internal static string NoSuchExtension { + get { + return ResourceManager.GetString("NoSuchExtension", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Not allowed to modify world settings.. + /// + internal static string NotAllowedToModifyWorldSettings { + get { + return ResourceManager.GetString("NotAllowedToModifyWorldSettings", resourceCulture); + } + } + /// /// Looks up a localized string similar to An attempt was made to perform an operation that requires a connection to a world server.. /// @@ -86,6 +194,15 @@ namespace VpSharp { } } + /// + /// Looks up a localized string similar to Radius must be greater than or equal to 1.. + /// + internal static string RadiusMustBeGreaterThan1 { + get { + return ResourceManager.GetString("RadiusMustBeGreaterThan1", resourceCulture); + } + } + /// /// Looks up a localized string similar to A specified value is too long.. /// @@ -95,6 +212,24 @@ namespace VpSharp { } } + /// + /// Looks up a localized string similar to Type cannot be abstract.. + /// + internal static string TypeCannotBeAbstract { + get { + return ResourceManager.GetString("TypeCannotBeAbstract", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Type must inherit from {0}.. + /// + internal static string TypeMustInherit { + get { + return ResourceManager.GetString("TypeMustInherit", resourceCulture); + } + } + /// /// Looks up a localized string similar to The specified remote endpoint is not supported.. /// @@ -104,6 +239,33 @@ namespace VpSharp { } } + /// + /// Looks up a localized string similar to Unsupported object type.. + /// + internal static string UnsupportedObjectType { + get { + return ResourceManager.GetString("UnsupportedObjectType", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Value cannot be empty.. + /// + internal static string ValueCannotBeEmpty { + get { + return ResourceManager.GetString("ValueCannotBeEmpty", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Connection to the universe server was lost, or connecting to the world failed.. + /// + internal static string WorldLoginError { + get { + return ResourceManager.GetString("WorldLoginError", resourceCulture); + } + } + /// /// Looks up a localized string similar to World name cannot be null or empty.. /// diff --git a/VpSharp/res/ExceptionMessages.resx b/VpSharp/res/ExceptionMessages.resx index c38b85e..efab419 100644 --- a/VpSharp/res/ExceptionMessages.resx +++ b/VpSharp/res/ExceptionMessages.resx @@ -24,21 +24,75 @@ + + The target avatar is not in the world. + + + Cannot invite non-existent user. + + + Cannot join non-existent user. + + + Cannot login due to incomplete configuration. + + + The specified username and password constitute an invalid login. + + + A value in the configuration is too long. (BotName?) + + + The login request timed out. + The operation cannot be performed on the client's current avatar. + + Connection to the server timed out. + + + A connection to a universe server is required for this operation. + + + A connection to a world server is required for this operation. + + + No extension with the type {0} is added to this client. + + + Not allowed to modify world settings. + An attempt was made to perform an operation that requires a connection to a world server. An attempt was made to perform an operation on a nonexistent object. + + Radius must be greater than or equal to 1. + A specified value is too long. + + Type cannot be abstract. + + + Type must inherit from {0}. + The specified remote endpoint is not supported. + + Unsupported object type. + + + Value cannot be empty. + + + Connection to the universe server was lost, or connecting to the world failed. + World name cannot be null or empty. diff --git a/VpSharp/src/Entities/VirtualParadiseAvatar.cs b/VpSharp/src/Entities/VirtualParadiseAvatar.cs index 242628a..6d11085 100644 --- a/VpSharp/src/Entities/VirtualParadiseAvatar.cs +++ b/VpSharp/src/Entities/VirtualParadiseAvatar.cs @@ -217,7 +217,7 @@ public sealed class VirtualParadiseAvatar : IEquatable ArgumentNullException.ThrowIfNull(message); if (string.IsNullOrWhiteSpace(message)) { - throw new ArgumentException("Message cannot be empty."); + throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(message)); } VirtualParadiseAvatar avatar; @@ -240,13 +240,13 @@ public sealed class VirtualParadiseAvatar : IEquatable switch (reason) { case ReasonCode.NotInWorld when _client.CurrentAvatar is null: - throw new InvalidOperationException("A connection to the world server is required to send messages."); + throw new InvalidOperationException(ExceptionMessages.ConnectionToWorldServerRequired); case ReasonCode.NotInWorld: - throw new InvalidOperationException("The recipient avatar is not in this world."); + throw new InvalidOperationException(ExceptionMessages.AvatarNotInWorld); case ReasonCode.StringTooLong: - throw new ArgumentException("The message is too long to send."); + throw new ArgumentException(ExceptionMessages.StringTooLong); } } @@ -354,7 +354,7 @@ public sealed class VirtualParadiseAvatar : IEquatable #else if (string.IsNullOrEmpty(world)) { - throw new ArgumentException("World cannot be empty"); + throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(world)); } #endif diff --git a/VpSharp/src/Entities/VirtualParadiseUser.cs b/VpSharp/src/Entities/VirtualParadiseUser.cs index 500e767..0aadcb9 100644 --- a/VpSharp/src/Entities/VirtualParadiseUser.cs +++ b/VpSharp/src/Entities/VirtualParadiseUser.cs @@ -156,9 +156,8 @@ public sealed class VirtualParadiseUser : IEquatable ReasonCode.Success => InviteResponse.Accepted, ReasonCode.InviteDeclined => InviteResponse.Declined, ReasonCode.Timeout => InviteResponse.TimeOut, - ReasonCode.NoSuchUser => throw new UserNotFoundException($"Cannot invite non-existent user {Id}."), - var _ => throw new InvalidOperationException( - $"An error occurred trying to invite the user: {reason:D} ({reason:G})") + ReasonCode.NoSuchUser => throw new UserNotFoundException(ExceptionMessages.CannotInviteNonExistentUser), + _ => throw new VirtualParadiseException(reason) }; } @@ -232,9 +231,8 @@ public sealed class VirtualParadiseUser : IEquatable ReasonCode.Success => JoinResponse.Accepted, ReasonCode.JoinDeclined => JoinResponse.Declined, ReasonCode.Timeout => JoinResponse.TimeOut, - ReasonCode.NoSuchUser => throw new UserNotFoundException($"Cannot join non-existent user {Id}."), - var _ => throw new InvalidOperationException( - $"An error occurred trying to join the user: {reason:D} ({reason:G})") + ReasonCode.NoSuchUser => throw new UserNotFoundException(ExceptionMessages.CannotJoinNonExistentUser), + _ => throw new VirtualParadiseException(reason) }; // ReSharper enable InconsistentlySynchronizedField diff --git a/VpSharp/src/VirtualParadiseClient.Extensions.cs b/VpSharp/src/VirtualParadiseClient.Extensions.cs index 7f5a6eb..43507e9 100644 --- a/VpSharp/src/VirtualParadiseClient.Extensions.cs +++ b/VpSharp/src/VirtualParadiseClient.Extensions.cs @@ -50,12 +50,14 @@ public sealed partial class VirtualParadiseClient if (!type.IsSubclassOf(typeof(VirtualParadiseClientExtension))) { - throw new ArgumentException($"Type must inherit from {typeof(VirtualParadiseClientExtension)}"); + throw new ArgumentException( + string.Format(CultureInfo.InvariantCulture, ExceptionMessages.TypeMustInherit, + typeof(VirtualParadiseClientExtension)), nameof(type)); } if (type.IsAbstract) { - throw new ArgumentException("Extension type cannot be abstract"); + throw new ArgumentException(ExceptionMessages.TypeCannotBeAbstract, nameof(type)); } object?[] argumentsActual = {this}; @@ -105,7 +107,8 @@ public sealed partial class VirtualParadiseClient var result = _extensions.Find(e => e.GetType() == typeof(T)) as T; if (result is null) { - throw new InvalidOperationException($"No extension with the type {typeof(T)} is added to this client."); + throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ExceptionMessages.NoSuchExtension, + typeof(T))); } return result; diff --git a/VpSharp/src/VirtualParadiseClient.Objects.cs b/VpSharp/src/VirtualParadiseClient.Objects.cs index a370ee2..774b7f6 100644 --- a/VpSharp/src/VirtualParadiseClient.Objects.cs +++ b/VpSharp/src/VirtualParadiseClient.Objects.cs @@ -54,7 +54,7 @@ public sealed partial class VirtualParadiseClient { if (radius < 0) { - throw new ArgumentException("Range must be greater than or equal to 1."); + throw new ArgumentException(ExceptionMessages.RadiusMustBeGreaterThan1, nameof(radius)); } var hashSet = new HashSet(); @@ -144,12 +144,10 @@ public sealed partial class VirtualParadiseClient PreReturn: return reason switch { - ReasonCode.DatabaseError => - throw new VirtualParadiseException(ReasonCode.DatabaseError, "Error communicating with the database."), + ReasonCode.DatabaseError => throw new VirtualParadiseException(ReasonCode.DatabaseError), ReasonCode.ObjectNotFound => throw new ObjectNotFoundException(), - ReasonCode.UnknownError => - throw new VirtualParadiseException(ReasonCode.UnknownError, "An unknown error occurred retrieving the object."), - _ when reason != ReasonCode.Success => throw new VirtualParadiseException(reason, $"{reason:D} ({reason:G})"), + ReasonCode.UnknownError => throw new VirtualParadiseException(ReasonCode.UnknownError), + _ when reason != ReasonCode.Success => throw new VirtualParadiseException(reason), _ => virtualParadiseObject! }; } @@ -224,7 +222,7 @@ public sealed partial class VirtualParadiseClient ObjectType.Model => new VirtualParadiseModelObject(this, id), ObjectType.ParticleEmitter => new VirtualParadiseParticleEmitterObject(this, id), ObjectType.Path => new VirtualParadisePathObject(this, id), - _ => throw new NotSupportedException("Unsupported object type.") + _ => throw new NotSupportedException(ExceptionMessages.UnsupportedObjectType) }; virtualParadiseObject.ExtractFromInstance(sender); diff --git a/VpSharp/src/VirtualParadiseClient.cs b/VpSharp/src/VirtualParadiseClient.cs index 55e10b7..5f65671 100644 --- a/VpSharp/src/VirtualParadiseClient.cs +++ b/VpSharp/src/VirtualParadiseClient.cs @@ -344,8 +344,7 @@ public sealed partial class VirtualParadiseClient : IDisposable break; case ReasonCode.NotInUniverse: - throw new InvalidOperationException( - "The client must be connected to the universe server in order to enter a world."); + throw new InvalidOperationException(ExceptionMessages.ConnectionToUniverseServerRequired); case ReasonCode.StringTooLong: ThrowHelper.ThrowStringTooLongException(nameof(worldName)); @@ -353,17 +352,16 @@ public sealed partial class VirtualParadiseClient : IDisposable case ReasonCode.ConnectionError: case ReasonCode.WorldLoginError: - throw new VirtualParadiseException(reason, - "Connection to the universe server was lost, or connecting to the world failed."); + throw new VirtualParadiseException(reason, ExceptionMessages.WorldLoginError); case ReasonCode.WorldNotFound: throw new WorldNotFoundException(worldName); case ReasonCode.Timeout: - throw new TimeoutException("Connection to the world server timed out."); + throw new TimeoutException(ExceptionMessages.ConnectionTimedOut); default: - throw new VirtualParadiseException(reason, $"Unknown error: {reason:D} ({reason:G})"); + throw new VirtualParadiseException(reason); } int size; @@ -483,7 +481,7 @@ public sealed partial class VirtualParadiseClient : IDisposable if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(botName)) { - throw new ArgumentException("Cannot login due to incomplete configuration."); + throw new ArgumentException(ExceptionMessages.CannotLogin_IncompleteConfiguration); } _loginCompletionSource = new TaskCompletionSource(); @@ -512,16 +510,16 @@ public sealed partial class VirtualParadiseClient : IDisposable switch (reason) { case ReasonCode.Timeout: - throw new TimeoutException("The login request timed out."); + throw new TimeoutException(ExceptionMessages.CannotLogin_Timeout); case ReasonCode.InvalidLogin: - throw new InvalidCredentialException("The specified username and password constitute an invalid login."); + throw new InvalidCredentialException(ExceptionMessages.CannotLogin_InvalidLogin); case ReasonCode.StringTooLong: - throw new ArgumentException($"A value in the configuration is too long. ({nameof(_configuration.BotName)}?)"); + throw new ArgumentException(ExceptionMessages.CannotLogin_StringTooLong); case ReasonCode.NotInUniverse: - throw new InvalidOperationException("A connection to the universe server is required to attempt login."); + throw new InvalidOperationException(ExceptionMessages.ConnectionToUniverseServerRequired); } int userId; @@ -554,7 +552,7 @@ public sealed partial class VirtualParadiseClient : IDisposable ArgumentNullException.ThrowIfNull(message); if (string.IsNullOrWhiteSpace(message)) { - throw new ArgumentException("Message cannot be empty."); + throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(message)); } lock (Lock) @@ -565,10 +563,10 @@ public sealed partial class VirtualParadiseClient : IDisposable switch (reason) { case ReasonCode.NotInWorld: - throw new InvalidOperationException("A connection to the world server is required to send messages."); + throw new InvalidOperationException(ExceptionMessages.ConnectionToWorldServerRequired); case ReasonCode.StringTooLong: - throw new ArgumentException("The message is too long to send."); + throw new ArgumentException(ExceptionMessages.StringTooLong); } } } diff --git a/VpSharp/src/WorldSettingsBuilder.cs b/VpSharp/src/WorldSettingsBuilder.cs index d1b9616..da4db84 100644 --- a/VpSharp/src/WorldSettingsBuilder.cs +++ b/VpSharp/src/WorldSettingsBuilder.cs @@ -396,7 +396,7 @@ public sealed class WorldSettingsBuilder if (reason == ReasonCode.NotAllowed) { - throw new UnauthorizedAccessException("Not allowed to modify world settings."); + throw new UnauthorizedAccessException(ExceptionMessages.NotAllowedToModifyWorldSettings); } } }