1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-23 00:28:48 +00:00

Move exception messages to resource file

This commit is contained in:
Oliver Booth 2022-12-06 02:35:37 +00:00
parent 148fd22c7d
commit 35684cccca
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
8 changed files with 249 additions and 36 deletions

View File

@ -59,6 +59,69 @@ namespace VpSharp {
} }
} }
/// <summary>
/// Looks up a localized string similar to The target avatar is not in the world..
/// </summary>
internal static string AvatarNotInWorld {
get {
return ResourceManager.GetString("AvatarNotInWorld", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Cannot invite non-existent user..
/// </summary>
internal static string CannotInviteNonExistentUser {
get {
return ResourceManager.GetString("CannotInviteNonExistentUser", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Cannot join non-existent user..
/// </summary>
internal static string CannotJoinNonExistentUser {
get {
return ResourceManager.GetString("CannotJoinNonExistentUser", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Cannot login due to incomplete configuration..
/// </summary>
internal static string CannotLogin_IncompleteConfiguration {
get {
return ResourceManager.GetString("CannotLogin_IncompleteConfiguration", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The specified username and password constitute an invalid login..
/// </summary>
internal static string CannotLogin_InvalidLogin {
get {
return ResourceManager.GetString("CannotLogin_InvalidLogin", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to A value in the configuration is too long. (BotName?).
/// </summary>
internal static string CannotLogin_StringTooLong {
get {
return ResourceManager.GetString("CannotLogin_StringTooLong", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The login request timed out..
/// </summary>
internal static string CannotLogin_Timeout {
get {
return ResourceManager.GetString("CannotLogin_Timeout", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to The operation cannot be performed on the client&apos;s current avatar.. /// Looks up a localized string similar to The operation cannot be performed on the client&apos;s current avatar..
/// </summary> /// </summary>
@ -68,6 +131,51 @@ namespace VpSharp {
} }
} }
/// <summary>
/// Looks up a localized string similar to Connection to the server timed out..
/// </summary>
internal static string ConnectionTimedOut {
get {
return ResourceManager.GetString("ConnectionTimedOut", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to A connection to a universe server is required for this operation..
/// </summary>
internal static string ConnectionToUniverseServerRequired {
get {
return ResourceManager.GetString("ConnectionToUniverseServerRequired", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to A connection to a world server is required for this operation..
/// </summary>
internal static string ConnectionToWorldServerRequired {
get {
return ResourceManager.GetString("ConnectionToWorldServerRequired", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to No extension with the type {0} is added to this client..
/// </summary>
internal static string NoSuchExtension {
get {
return ResourceManager.GetString("NoSuchExtension", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Not allowed to modify world settings..
/// </summary>
internal static string NotAllowedToModifyWorldSettings {
get {
return ResourceManager.GetString("NotAllowedToModifyWorldSettings", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to An attempt was made to perform an operation that requires a connection to a world server.. /// Looks up a localized string similar to An attempt was made to perform an operation that requires a connection to a world server..
/// </summary> /// </summary>
@ -86,6 +194,15 @@ namespace VpSharp {
} }
} }
/// <summary>
/// Looks up a localized string similar to Radius must be greater than or equal to 1..
/// </summary>
internal static string RadiusMustBeGreaterThan1 {
get {
return ResourceManager.GetString("RadiusMustBeGreaterThan1", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to A specified value is too long.. /// Looks up a localized string similar to A specified value is too long..
/// </summary> /// </summary>
@ -95,6 +212,24 @@ namespace VpSharp {
} }
} }
/// <summary>
/// Looks up a localized string similar to Type cannot be abstract..
/// </summary>
internal static string TypeCannotBeAbstract {
get {
return ResourceManager.GetString("TypeCannotBeAbstract", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Type must inherit from {0}..
/// </summary>
internal static string TypeMustInherit {
get {
return ResourceManager.GetString("TypeMustInherit", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to The specified remote endpoint is not supported.. /// Looks up a localized string similar to The specified remote endpoint is not supported..
/// </summary> /// </summary>
@ -104,6 +239,33 @@ namespace VpSharp {
} }
} }
/// <summary>
/// Looks up a localized string similar to Unsupported object type..
/// </summary>
internal static string UnsupportedObjectType {
get {
return ResourceManager.GetString("UnsupportedObjectType", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Value cannot be empty..
/// </summary>
internal static string ValueCannotBeEmpty {
get {
return ResourceManager.GetString("ValueCannotBeEmpty", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Connection to the universe server was lost, or connecting to the world failed..
/// </summary>
internal static string WorldLoginError {
get {
return ResourceManager.GetString("WorldLoginError", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to World name cannot be null or empty.. /// Looks up a localized string similar to World name cannot be null or empty..
/// </summary> /// </summary>

View File

@ -24,21 +24,75 @@
</value> </value>
</resheader> </resheader>
<data name="AvatarNotInWorld" xml:space="preserve">
<value>The target avatar is not in the world.</value>
</data>
<data name="CannotInviteNonExistentUser" xml:space="preserve">
<value>Cannot invite non-existent user.</value>
</data>
<data name="CannotJoinNonExistentUser" xml:space="preserve">
<value>Cannot join non-existent user.</value>
</data>
<data name="CannotLogin_IncompleteConfiguration" xml:space="preserve">
<value>Cannot login due to incomplete configuration.</value>
</data>
<data name="CannotLogin_InvalidLogin" xml:space="preserve">
<value>The specified username and password constitute an invalid login.</value>
</data>
<data name="CannotLogin_StringTooLong" xml:space="preserve">
<value>A value in the configuration is too long. (BotName?)</value>
</data>
<data name="CannotLogin_Timeout" xml:space="preserve">
<value>The login request timed out.</value>
</data>
<data name="CannotUseSelf" xml:space="preserve"> <data name="CannotUseSelf" xml:space="preserve">
<value>The operation cannot be performed on the client's current avatar.</value> <value>The operation cannot be performed on the client's current avatar.</value>
</data> </data>
<data name="ConnectionTimedOut" xml:space="preserve">
<value>Connection to the server timed out.</value>
</data>
<data name="ConnectionToUniverseServerRequired" xml:space="preserve">
<value>A connection to a universe server is required for this operation.</value>
</data>
<data name="ConnectionToWorldServerRequired" xml:space="preserve">
<value>A connection to a world server is required for this operation.</value>
</data>
<data name="NoSuchExtension" xml:space="preserve">
<value>No extension with the type {0} is added to this client.</value>
</data>
<data name="NotAllowedToModifyWorldSettings" xml:space="preserve">
<value>Not allowed to modify world settings.</value>
</data>
<data name="NotInWorld" xml:space="preserve"> <data name="NotInWorld" xml:space="preserve">
<value>An attempt was made to perform an operation that requires a connection to a world server.</value> <value>An attempt was made to perform an operation that requires a connection to a world server.</value>
</data> </data>
<data name="ObjectNotFound" xml:space="preserve"> <data name="ObjectNotFound" xml:space="preserve">
<value>An attempt was made to perform an operation on a nonexistent object.</value> <value>An attempt was made to perform an operation on a nonexistent object.</value>
</data> </data>
<data name="RadiusMustBeGreaterThan1" xml:space="preserve">
<value>Radius must be greater than or equal to 1.</value>
</data>
<data name="StringTooLong" xml:space="preserve"> <data name="StringTooLong" xml:space="preserve">
<value>A specified value is too long.</value> <value>A specified value is too long.</value>
</data> </data>
<data name="TypeCannotBeAbstract" xml:space="preserve">
<value>Type cannot be abstract.</value>
</data>
<data name="TypeMustInherit" xml:space="preserve">
<value>Type must inherit from {0}.</value>
</data>
<data name="UnsupportedEndpoint" xml:space="preserve"> <data name="UnsupportedEndpoint" xml:space="preserve">
<value>The specified remote endpoint is not supported.</value> <value>The specified remote endpoint is not supported.</value>
</data> </data>
<data name="UnsupportedObjectType" xml:space="preserve">
<value>Unsupported object type.</value>
</data>
<data name="ValueCannotBeEmpty" xml:space="preserve">
<value>Value cannot be empty.</value>
</data>
<data name="WorldLoginError" xml:space="preserve">
Connection to the universe server was lost, or connecting to the world failed.
</data>
<data name="WorldNameCannotBeEmpty" xml:space="preserve"> <data name="WorldNameCannotBeEmpty" xml:space="preserve">
<value>World name cannot be null or empty.</value> <value>World name cannot be null or empty.</value>
</data> </data>

View File

@ -217,7 +217,7 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
ArgumentNullException.ThrowIfNull(message); ArgumentNullException.ThrowIfNull(message);
if (string.IsNullOrWhiteSpace(message)) if (string.IsNullOrWhiteSpace(message))
{ {
throw new ArgumentException("Message cannot be empty."); throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(message));
} }
VirtualParadiseAvatar avatar; VirtualParadiseAvatar avatar;
@ -240,13 +240,13 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
switch (reason) switch (reason)
{ {
case ReasonCode.NotInWorld when _client.CurrentAvatar is null: 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: case ReasonCode.NotInWorld:
throw new InvalidOperationException("The recipient avatar is not in this world."); throw new InvalidOperationException(ExceptionMessages.AvatarNotInWorld);
case ReasonCode.StringTooLong: 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<VirtualParadiseAvatar>
#else #else
if (string.IsNullOrEmpty(world)) if (string.IsNullOrEmpty(world))
{ {
throw new ArgumentException("World cannot be empty"); throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(world));
} }
#endif #endif

View File

@ -156,9 +156,8 @@ public sealed class VirtualParadiseUser : IEquatable<VirtualParadiseUser>
ReasonCode.Success => InviteResponse.Accepted, ReasonCode.Success => InviteResponse.Accepted,
ReasonCode.InviteDeclined => InviteResponse.Declined, ReasonCode.InviteDeclined => InviteResponse.Declined,
ReasonCode.Timeout => InviteResponse.TimeOut, ReasonCode.Timeout => InviteResponse.TimeOut,
ReasonCode.NoSuchUser => throw new UserNotFoundException($"Cannot invite non-existent user {Id}."), ReasonCode.NoSuchUser => throw new UserNotFoundException(ExceptionMessages.CannotInviteNonExistentUser),
var _ => throw new InvalidOperationException( _ => throw new VirtualParadiseException(reason)
$"An error occurred trying to invite the user: {reason:D} ({reason:G})")
}; };
} }
@ -232,9 +231,8 @@ public sealed class VirtualParadiseUser : IEquatable<VirtualParadiseUser>
ReasonCode.Success => JoinResponse.Accepted, ReasonCode.Success => JoinResponse.Accepted,
ReasonCode.JoinDeclined => JoinResponse.Declined, ReasonCode.JoinDeclined => JoinResponse.Declined,
ReasonCode.Timeout => JoinResponse.TimeOut, ReasonCode.Timeout => JoinResponse.TimeOut,
ReasonCode.NoSuchUser => throw new UserNotFoundException($"Cannot join non-existent user {Id}."), ReasonCode.NoSuchUser => throw new UserNotFoundException(ExceptionMessages.CannotJoinNonExistentUser),
var _ => throw new InvalidOperationException( _ => throw new VirtualParadiseException(reason)
$"An error occurred trying to join the user: {reason:D} ({reason:G})")
}; };
// ReSharper enable InconsistentlySynchronizedField // ReSharper enable InconsistentlySynchronizedField

View File

@ -50,12 +50,14 @@ public sealed partial class VirtualParadiseClient
if (!type.IsSubclassOf(typeof(VirtualParadiseClientExtension))) 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) if (type.IsAbstract)
{ {
throw new ArgumentException("Extension type cannot be abstract"); throw new ArgumentException(ExceptionMessages.TypeCannotBeAbstract, nameof(type));
} }
object?[] argumentsActual = {this}; object?[] argumentsActual = {this};
@ -105,7 +107,8 @@ public sealed partial class VirtualParadiseClient
var result = _extensions.Find(e => e.GetType() == typeof(T)) as T; var result = _extensions.Find(e => e.GetType() == typeof(T)) as T;
if (result is null) 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; return result;

View File

@ -54,7 +54,7 @@ public sealed partial class VirtualParadiseClient
{ {
if (radius < 0) 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<Cell>(); var hashSet = new HashSet<Cell>();
@ -144,12 +144,10 @@ public sealed partial class VirtualParadiseClient
PreReturn: PreReturn:
return reason switch return reason switch
{ {
ReasonCode.DatabaseError => ReasonCode.DatabaseError => throw new VirtualParadiseException(ReasonCode.DatabaseError),
throw new VirtualParadiseException(ReasonCode.DatabaseError, "Error communicating with the database."),
ReasonCode.ObjectNotFound => throw new ObjectNotFoundException(), ReasonCode.ObjectNotFound => throw new ObjectNotFoundException(),
ReasonCode.UnknownError => ReasonCode.UnknownError => throw new VirtualParadiseException(ReasonCode.UnknownError),
throw new VirtualParadiseException(ReasonCode.UnknownError, "An unknown error occurred retrieving the object."), _ when reason != ReasonCode.Success => throw new VirtualParadiseException(reason),
_ when reason != ReasonCode.Success => throw new VirtualParadiseException(reason, $"{reason:D} ({reason:G})"),
_ => virtualParadiseObject! _ => virtualParadiseObject!
}; };
} }
@ -224,7 +222,7 @@ public sealed partial class VirtualParadiseClient
ObjectType.Model => new VirtualParadiseModelObject(this, id), ObjectType.Model => new VirtualParadiseModelObject(this, id),
ObjectType.ParticleEmitter => new VirtualParadiseParticleEmitterObject(this, id), ObjectType.ParticleEmitter => new VirtualParadiseParticleEmitterObject(this, id),
ObjectType.Path => new VirtualParadisePathObject(this, id), ObjectType.Path => new VirtualParadisePathObject(this, id),
_ => throw new NotSupportedException("Unsupported object type.") _ => throw new NotSupportedException(ExceptionMessages.UnsupportedObjectType)
}; };
virtualParadiseObject.ExtractFromInstance(sender); virtualParadiseObject.ExtractFromInstance(sender);

View File

@ -344,8 +344,7 @@ public sealed partial class VirtualParadiseClient : IDisposable
break; break;
case ReasonCode.NotInUniverse: case ReasonCode.NotInUniverse:
throw new InvalidOperationException( throw new InvalidOperationException(ExceptionMessages.ConnectionToUniverseServerRequired);
"The client must be connected to the universe server in order to enter a world.");
case ReasonCode.StringTooLong: case ReasonCode.StringTooLong:
ThrowHelper.ThrowStringTooLongException(nameof(worldName)); ThrowHelper.ThrowStringTooLongException(nameof(worldName));
@ -353,17 +352,16 @@ public sealed partial class VirtualParadiseClient : IDisposable
case ReasonCode.ConnectionError: case ReasonCode.ConnectionError:
case ReasonCode.WorldLoginError: case ReasonCode.WorldLoginError:
throw new VirtualParadiseException(reason, throw new VirtualParadiseException(reason, ExceptionMessages.WorldLoginError);
"Connection to the universe server was lost, or connecting to the world failed.");
case ReasonCode.WorldNotFound: case ReasonCode.WorldNotFound:
throw new WorldNotFoundException(worldName); throw new WorldNotFoundException(worldName);
case ReasonCode.Timeout: case ReasonCode.Timeout:
throw new TimeoutException("Connection to the world server timed out."); throw new TimeoutException(ExceptionMessages.ConnectionTimedOut);
default: default:
throw new VirtualParadiseException(reason, $"Unknown error: {reason:D} ({reason:G})"); throw new VirtualParadiseException(reason);
} }
int size; int size;
@ -483,7 +481,7 @@ public sealed partial class VirtualParadiseClient : IDisposable
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(botName)) 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<ReasonCode>(); _loginCompletionSource = new TaskCompletionSource<ReasonCode>();
@ -512,16 +510,16 @@ public sealed partial class VirtualParadiseClient : IDisposable
switch (reason) switch (reason)
{ {
case ReasonCode.Timeout: case ReasonCode.Timeout:
throw new TimeoutException("The login request timed out."); throw new TimeoutException(ExceptionMessages.CannotLogin_Timeout);
case ReasonCode.InvalidLogin: case ReasonCode.InvalidLogin:
throw new InvalidCredentialException("The specified username and password constitute an invalid login."); throw new InvalidCredentialException(ExceptionMessages.CannotLogin_InvalidLogin);
case ReasonCode.StringTooLong: 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: case ReasonCode.NotInUniverse:
throw new InvalidOperationException("A connection to the universe server is required to attempt login."); throw new InvalidOperationException(ExceptionMessages.ConnectionToUniverseServerRequired);
} }
int userId; int userId;
@ -554,7 +552,7 @@ public sealed partial class VirtualParadiseClient : IDisposable
ArgumentNullException.ThrowIfNull(message); ArgumentNullException.ThrowIfNull(message);
if (string.IsNullOrWhiteSpace(message)) if (string.IsNullOrWhiteSpace(message))
{ {
throw new ArgumentException("Message cannot be empty."); throw new ArgumentException(ExceptionMessages.ValueCannotBeEmpty, nameof(message));
} }
lock (Lock) lock (Lock)
@ -565,10 +563,10 @@ public sealed partial class VirtualParadiseClient : IDisposable
switch (reason) switch (reason)
{ {
case ReasonCode.NotInWorld: 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: case ReasonCode.StringTooLong:
throw new ArgumentException("The message is too long to send."); throw new ArgumentException(ExceptionMessages.StringTooLong);
} }
} }
} }

View File

@ -396,7 +396,7 @@ public sealed class WorldSettingsBuilder
if (reason == ReasonCode.NotAllowed) if (reason == ReasonCode.NotAllowed)
{ {
throw new UnauthorizedAccessException("Not allowed to modify world settings."); throw new UnauthorizedAccessException(ExceptionMessages.NotAllowedToModifyWorldSettings);
} }
} }
} }