From 10af146ccfb921712eecd27ac5bc64cb66bf5c80 Mon Sep 17 00:00:00 2001 From: rinrab Date: Wed, 10 Jan 2024 19:48:13 +0100 Subject: [PATCH] Refactoring: move handling of NotFound and AlreadyExists exceptions from ConvertCimException() to CreateVpnConnection() and DeleteVpnConnection(). ConvertCimException now just format error message and writes it to Exception. --- AOVpnManager/VpnManager.cs | 45 ++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/AOVpnManager/VpnManager.cs b/AOVpnManager/VpnManager.cs index 4adb2ee..6e5194f 100644 --- a/AOVpnManager/VpnManager.cs +++ b/AOVpnManager/VpnManager.cs @@ -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) @@ -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) @@ -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)