Skip to content

Commit

Permalink
Refactoring: move handling of NotFound and AlreadyExists exceptions f…
Browse files Browse the repository at this point in the history
…rom ConvertCimException() to CreateVpnConnection() and DeleteVpnConnection(). ConvertCimException now just format error message and writes it to Exception.
  • Loading branch information
rinrab committed Jan 10, 2024
1 parent 38d658d commit 10af146
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions AOVpnManager/VpnManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,21 @@ public void CreateVpnConnection(string connectionName, string profile)
{
AddKeyPropertiesToVpnConnection(newInstance, connectionName);
AddValuePropertiesToVpnConnection(newInstance, profile);
session.CreateInstance(NamespaceName, newInstance);
try
{
session.CreateInstance(NamespaceName, newInstance);
}
catch (CimException ex)
{
if (ex.NativeErrorCode == NativeErrorCode.AlreadyExists)
{
throw new VpnConnectionNotFoundException(connectionName);
}
else
{
throw;
}
}
}
}
catch (CimException ex)
Expand All @@ -52,7 +66,21 @@ public void DeleteVpnConnection(string connectionName)
using (CimInstance queryInstance = new CimInstance(ClassName, NamespaceName))
{
AddKeyPropertiesToVpnConnection(queryInstance, connectionName);
session.DeleteInstance(queryInstance);
try
{
session.DeleteInstance(queryInstance);
}
catch (CimException ex)
{
if (ex.NativeErrorCode == NativeErrorCode.NotFound)
{
throw new VpnConnectionNotFoundException(connectionName);
}
else
{
throw;
}
}
}
}
catch (CimException ex)
Expand All @@ -68,18 +96,7 @@ public void Dispose()

private static Exception ConvertCimException(CimException ex)
{
if (ex.NativeErrorCode == NativeErrorCode.NotFound)
{
return new VpnConnectionNotFoundException();
}
else if (ex.NativeErrorCode == NativeErrorCode.AlreadyExists)
{
return new VpnConnectionAlreadyExistsException();
}
else
{
return new Exception(string.Format("{0} ({1})", ex.Message, ex.NativeErrorCode), ex);
}
return new Exception(string.Format("{0} ({1})", ex.Message, ex.NativeErrorCode), ex);
}

private static void AddKeyPropertiesToVpnConnection(CimInstance instance, string connectionName)
Expand Down

0 comments on commit 10af146

Please sign in to comment.