Problem with Publish-AdfV2FromJson not yet failing even using errorActionPreference: stop#490
Conversation
Publish-AdfV2FromJson not yet failing even using errorActionPreference: stop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed
Publish-AdfV2FromJsonno longer silently continues after errors: config path errors (ADFT0010) and deployment failures (e.g. Azure Policy blocks) now propagate as terminating errors to the caller Publish-AdfV2FromJson not yet failing even using errorActionPreference: stop #472Problem
Publish-AdfV2FromJsonwas silently continuing after errors and always printing"Azure Data Factory files have been deployed successfully."even when:ADFT0010), andRequestDisallowedByPolicy).Setting
$ErrorActionPreference = 'Stop'in the calling script had no effect because module functions execute in the module scope, which does not inherit preference variables from the caller's script scope.Two compounding root causes:
Write-Erroris non-terminating — errors were written to the error stream but execution continued normally, so the calling script had no way to detect the failure.ForEach-Objectpipeline absorbs terminating errors in PowerShell 5.1 — even genuinely terminating errors thrown by Azure cmdlets (e.g.New-AzResourceblocked by policy) were swallowed by the pipeline and did not propagate to the caller.Changes
New test file —
test/Publish-AdfV2FromJson-ErrorHandling.Tests.ps1Four unit tests that reproduce the bug and verify the fix, requiring no live Azure connection:
-DryRun)Publish-AdfV2FromJsonFailsWhenPathNotFound = $falseShould -Throw)ForEach-ObjectShould -Throw(explicit try/catch)$ErrorActionPreferenceAll four tests were red before the fix and are green after.
private/Update-PropertiesFromFile.ps1Write-Error "ADFT0007: ..."→throw— object not found in config (whenFailsWhenConfigItemNotFound = $true)Write-Error -Exception $exc→throw $exc— invalid property path ADFT0010 (whenFailsWhenPathNotFound = $true)private/Deploy-AdfObject.ps1Write-Error "ADFT0005: ..."→throw— referenced dependency object not found (whenIgnoreLackOfReferencedObject = $false)private/Deploy-AdfObjectOnly.ps1Write-Error "ADFT0012: ..."→throw— unsupported Integration Runtime typeWrite-Error "ADFT0013: ..."→throw— unsupported ADF object typepublic/Publish-AdfV2FromJson.ps1$adf.AllObjects() | ForEach-Object { Deploy-AdfObject -obj $_ }→foreach ($obj in $adf.AllObjects()) { Deploy-AdfObject -obj $obj }Remove-AdfObjectIfNotInSourceThe
foreachstatement propagates terminating errors from Azure cmdlets correctly in all PowerShell versions;ForEach-Object(a pipeline cmdlet) was absorbing them in PowerShell 5.1.changelog.mdNew entry added under
## Unreleased.Why
throwand not$PSCmdlet.ThrowTerminatingError()throwproduces an exception that propagates through every layer of the call stack — includingForEach-Objectpipelines and across module scope boundaries — regardless of$ErrorActionPreference.Write-Erroronly becomes terminating if$ErrorActionPreference = 'Stop'is set in the module's own scope, which the caller cannot control. Usingthrowat the error source removes this dependency entirely.