Skip to content

Commit

Permalink
Fix exception catching regression
Browse files Browse the repository at this point in the history
  • Loading branch information
daxian-dbw committed Apr 7, 2021
1 parent 2dadda3 commit caf86e5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/System.Management.Automation/engine/CommandProcessor.cs
Expand Up @@ -324,7 +324,7 @@ internal override void ProcessRecord()
Command.DoBeginProcessing();
}
}
catch (Exception e) when (ShallWrapBeforeBubbleUp(e))
catch (Exception e)
{
// This cmdlet threw an exception, so wrap it and bubble it up.
throw ManageInvocationException(e);
Expand Down Expand Up @@ -394,7 +394,13 @@ internal override void ProcessRecord()

exceptionToThrow = rte;
}
catch (Exception e) when (ShallWrapBeforeBubbleUp(e))
catch (LoopFlowException)
{
// Don't wrap LoopFlowException, we incorrectly raise a PipelineStoppedException
// which gets caught by a script try/catch if we wrap here.
throw;
}
catch (Exception e)
{
// Catch-all OK, 3rd party callout.
exceptionToThrow = e;
Expand Down
20 changes: 11 additions & 9 deletions src/System.Management.Automation/engine/CommandProcessorBase.cs
Expand Up @@ -536,7 +536,7 @@ internal virtual void DoBegin()
Command.DoBeginProcessing();
}
}
catch (Exception e) when (ShallWrapBeforeBubbleUp(e))
catch (Exception e)
{
// This cmdlet threw an exception, so
// wrap it and bubble it up.
Expand Down Expand Up @@ -580,12 +580,6 @@ internal void DoExecute()
}
}

protected bool ShallWrapBeforeBubbleUp(Exception e)
{
return e is not FlowControlException
&& e is not HaltCommandException;
}

/// <summary>
/// Called once after ProcessRecord().
/// Internally it calls EndProcessing() of the InternalCommand.
Expand All @@ -606,7 +600,7 @@ internal virtual void Complete()
this.Command.DoEndProcessing();
}
}
catch (Exception e) when (ShallWrapBeforeBubbleUp(e))
catch (Exception e)
{
// This cmdlet threw an exception, wrap it as needed and bubble it up.
throw ManageInvocationException(e);
Expand Down Expand Up @@ -665,7 +659,15 @@ protected virtual void CleanResource()
this.Command.DoCleanResource();
}
}
catch (Exception e) when (ShallWrapBeforeBubbleUp(e))
catch (HaltCommandException)
{
throw;
}
catch (FlowControlException)
{
throw;
}
catch (Exception e)
{
// This cmdlet threw an exception, so wrap it and bubble it up.
throw ManageInvocationException(e);
Expand Down
Expand Up @@ -607,12 +607,16 @@ private void RunClause(Action<FunctionContext> clause, object dollarUnderbar, ob
if (exitCode != 0)
this.commandRuntime.PipelineProcessor.ExecutionFailed = true;
}
catch (FlowControlException)
{
throw;
}
catch (RuntimeException e)
{
// This method always throws.
ManageScriptException(e);
}
catch (Exception e) when (ShallWrapBeforeBubbleUp(e))
catch (Exception e)
{
// This cmdlet threw an exception, so wrap it and bubble it up.
throw ManageInvocationException(e);
Expand Down

0 comments on commit caf86e5

Please sign in to comment.