Skip to content

Commit 2d83b64

Browse files
committed
Get rid of awkward ExceptionDispatchInfo setup in DeploymentSession.
1 parent 0059698 commit 2d83b64

21 files changed

Lines changed: 14 additions & 34 deletions

src/PSADT/PSAppDeployToolkit/Foundation/DeploymentSession.cs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Management.Automation;
1111
using System.Reflection;
1212
using System.Runtime.CompilerServices;
13-
using System.Runtime.ExceptionServices;
1413
using System.Runtime.InteropServices;
1514
using System.Security.Principal;
1615
using System.Text.RegularExpressions;
@@ -42,6 +41,7 @@ public class DeploymentSession
4241
/// <param name="parameters">All parameters from Open-ADTSession.</param>
4342
/// <param name="noExitOnClose">Indicates that the shell shouldn't exit on the last session closure.</param>
4443
/// <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.")]
4545
public DeploymentSession(IReadOnlyDictionary<string, object>? parameters = null, bool? noExitOnClose = null, bool? compatibilityMode = null)
4646
{
4747
try
@@ -1014,20 +1014,13 @@ public DeploymentSession(IReadOnlyDictionary<string, object>? parameters = null,
10141014

10151015
#endregion
10161016
}
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-
}
10271017
catch (Exception ex)
10281018
{
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);
10311024
}
10321025
}
10331026

@@ -1424,26 +1417,6 @@ public void AddMountedWimFile(FileInfo wimFile)
14241417
MountedWimFiles.Add(wimFile);
14251418
}
14261419

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-
14471420
/// <summary>
14481421
/// Writes a log divider.
14491422
/// </summary>

src/PSAppDeployToolkit/Public/Open-ADTSession.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,14 @@ function Open-ADTSession
494494
}
495495
catch
496496
{
497-
Write-Error -Exception $_.Exception.InnerException -Category OpenError -CategoryTargetName $Script:ADT.LastExitCode -CategoryTargetType $Script:ADT.LastExitCode.GetType().Name
497+
if ($_.Exception.InnerException -is [System.ApplicationException])
498+
{
499+
Write-Error -Exception $_.Exception.InnerException.InnerException -Category OpenError -CategoryTargetName $Script:ADT.LastExitCode -CategoryTargetType $Script:ADT.LastExitCode.GetType().Name
500+
}
501+
else
502+
{
503+
Write-Error -ErrorRecord $_
504+
}
498505
}
499506
}
500507
catch
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)