|
10 | 10 | using System.Management.Automation; |
11 | 11 | using System.Reflection; |
12 | 12 | using System.Runtime.CompilerServices; |
13 | | -using System.Runtime.ExceptionServices; |
14 | 13 | using System.Runtime.InteropServices; |
15 | 14 | using System.Security.Principal; |
16 | 15 | using System.Text.RegularExpressions; |
@@ -42,6 +41,7 @@ public class DeploymentSession |
42 | 41 | /// <param name="parameters">All parameters from Open-ADTSession.</param> |
43 | 42 | /// <param name="noExitOnClose">Indicates that the shell shouldn't exit on the last session closure.</param> |
44 | 43 | /// <param name="compatibilityMode">Indicates whether compatibility mode is enabled.</param> |
| 44 | + [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "This exception type is fine here.")] |
45 | 45 | public DeploymentSession(IReadOnlyDictionary<string, object>? parameters = null, bool? noExitOnClose = null, bool? compatibilityMode = null) |
46 | 46 | { |
47 | 47 | try |
@@ -1014,20 +1014,13 @@ public DeploymentSession(IReadOnlyDictionary<string, object>? parameters = null, |
1014 | 1014 |
|
1015 | 1015 | #endregion |
1016 | 1016 | } |
1017 | | - catch (UnauthorizedAccessException ex) |
1018 | | - { |
1019 | | - HandleCtorException(ex, ex.Message, 60008); |
1020 | | - throw; |
1021 | | - } |
1022 | | - catch (NotSupportedException ex) |
1023 | | - { |
1024 | | - HandleCtorException(ex, ex.Message, DeferExitCode); |
1025 | | - throw; |
1026 | | - } |
1027 | 1017 | catch (Exception ex) |
1028 | 1018 | { |
1029 | | - HandleCtorException(ex, $"Failure occurred while instantiating new deployment session: {ex}", 60008); |
1030 | | - throw; |
| 1019 | + WriteLogEntry(ex.Message, LogSeverity.Error); |
| 1020 | + RemoveSubstDrive(); DismountWimFiles(); |
| 1021 | + SetExitCode(ex is NotSupportedException ? DeferExitCode : 60008); |
| 1022 | + Environment.ExitCode = Close(); |
| 1023 | + throw new ApplicationException("Failure occurred while instantiating new deployment session.", ex); |
1031 | 1024 | } |
1032 | 1025 | } |
1033 | 1026 |
|
@@ -1424,26 +1417,6 @@ public void AddMountedWimFile(FileInfo wimFile) |
1424 | 1417 | MountedWimFiles.Add(wimFile); |
1425 | 1418 | } |
1426 | 1419 |
|
1427 | | - /// <summary> |
1428 | | - /// Handles an exception that occurs during object construction by logging the error, performing cleanup, |
1429 | | - /// setting the process exit code, and rethrowing the exception. |
1430 | | - /// </summary> |
1431 | | - /// <remarks>This method performs necessary cleanup and ensures that the process exit code is set |
1432 | | - /// before rethrowing the original exception. The method does not return to the caller, as it rethrows the |
1433 | | - /// exception after cleanup.</remarks> |
1434 | | - /// <param name="ex">The exception that was thrown during construction. Cannot be null.</param> |
1435 | | - /// <param name="logMessage">The message to log describing the error condition.</param> |
1436 | | - /// <param name="exitCode">The exit code to set for the process before termination.</param> |
1437 | | - private void HandleCtorException(Exception ex, string logMessage, int exitCode) |
1438 | | - { |
1439 | | - WriteLogEntry(logMessage, LogSeverity.Error); |
1440 | | - RemoveSubstDrive(); |
1441 | | - DismountWimFiles(); |
1442 | | - SetExitCode(exitCode); |
1443 | | - Environment.ExitCode = Close(); |
1444 | | - ExceptionDispatchInfo.Capture(ex).Throw(); |
1445 | | - } |
1446 | | - |
1447 | 1420 | /// <summary> |
1448 | 1421 | /// Writes a log divider. |
1449 | 1422 | /// </summary> |
|
0 commit comments