Refactor error messages into constants

Co-authored-by: tig <585482+tig@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-22 18:55:47 +00:00
parent 25fdeef61a
commit bf74866e81
2 changed files with 18 additions and 12 deletions

View File

@@ -27,17 +27,13 @@ public partial class ApplicationImpl
// If this is a legacy static instance and instance-based model was used, throw
if (this == _instance && _modelUsage == ApplicationModelUsage.InstanceBased)
{
throw new InvalidOperationException (
"Cannot use legacy static Application model (Application.Init/ApplicationImpl.Instance) after using modern instance-based model (Application.Create). " +
"Use only one model per process.");
throw new InvalidOperationException (ErrorLegacyAfterModern);
}
// If this is an instance-based instance and legacy static model was used, throw
if (this != _instance && _modelUsage == ApplicationModelUsage.LegacyStatic)
{
throw new InvalidOperationException (
"Cannot use modern instance-based model (Application.Create) after using legacy static Application model (Application.Init/ApplicationImpl.Instance). " +
"Use only one model per process.");
throw new InvalidOperationException (ErrorModernAfterLegacy);
}
if (!string.IsNullOrWhiteSpace (driverName))

View File

@@ -26,6 +26,20 @@ public partial class ApplicationImpl : IApplication
/// </summary>
private static ApplicationModelUsage _modelUsage = ApplicationModelUsage.None;
/// <summary>
/// Error message for when trying to use modern model after legacy static model.
/// </summary>
private const string ErrorModernAfterLegacy =
"Cannot use modern instance-based model (Application.Create) after using legacy static Application model (Application.Init/ApplicationImpl.Instance). " +
"Use only one model per process.";
/// <summary>
/// Error message for when trying to use legacy static model after modern model.
/// </summary>
private const string ErrorLegacyAfterModern =
"Cannot use legacy static Application model (Application.Init/ApplicationImpl.Instance) after using modern instance-based model (Application.Create). " +
"Use only one model per process.";
/// <summary>
/// Configures the singleton instance of <see cref="Application"/> to use the specified backend implementation.
/// </summary>
@@ -52,9 +66,7 @@ public partial class ApplicationImpl : IApplication
// Check if the instance-based model has already been used
if (_modelUsage == ApplicationModelUsage.InstanceBased)
{
throw new InvalidOperationException (
"Cannot use legacy static Application model (Application.Init/ApplicationImpl.Instance) after using modern instance-based model (Application.Create). " +
"Use only one model per process.");
throw new InvalidOperationException (ErrorLegacyAfterModern);
}
// Mark the usage and create the instance
@@ -72,9 +84,7 @@ public partial class ApplicationImpl : IApplication
// Check if the legacy static model has already been initialized
if (_modelUsage == ApplicationModelUsage.LegacyStatic && _instance?.Initialized == true)
{
throw new InvalidOperationException (
"Cannot use modern instance-based model (Application.Create) after using legacy static Application model (Application.Init/ApplicationImpl.Instance). " +
"Use only one model per process.");
throw new InvalidOperationException (ErrorModernAfterLegacy);
}
_modelUsage = ApplicationModelUsage.InstanceBased;