@@ -349,6 +349,36 @@ function SetDeploymentOutputs(
349349 return $deploymentEnvironmentVariables , $deploymentOutputs
350350}
351351
352+ <#
353+ Writes Resource Manager errors which often have nested exceptions.
354+ https://learn.microsoft.com/dotnet/api/microsoft.azure.commands.resourcemanager.cmdlets.sdkmodels.psresourcemanagererror
355+ #>
356+ function Write-PSResourceManagerError ($resourceManagerError , [int ]$level , [int ]$maxLevel ) {
357+ if (! $resourceManagerError -or ! $resourceManagerError.Message ) {
358+ return ;
359+ }
360+
361+ # Retrieve one or more messages then decode the strings for readability (remove quote escapes, fix link readability, etc.)
362+ $parsedMessage = ($resourceManagerError.Message -join " $ ( [System.Environment ]::NewLine) $ ( [System.Environment ]::NewLine) " )
363+ $parsedMessage = [System.Net.WebUtility ]::UrlDecode($parsedMessage )
364+
365+ $prefix = " " * ($level * 2 ) + " -"
366+ Write-Host " $prefix $parsedMessage "
367+
368+ # Limit the level of nested exceptions to prevent overwhelming users with details and infinite recursive calls.
369+ if ($level -ge $maxLevel ) {
370+ if ($resourceManagerError.Details.Count -gt 0 ) {
371+ Write-Host " $prefix ... (additional nested errors not shown)"
372+ }
373+
374+ return ;
375+ }
376+
377+ foreach ($detail in $resourceManagerError.Details ) {
378+ Write-PSResourceManagerError $detail ($level + 1 ) $maxLevel
379+ }
380+ }
381+
352382function HandleTemplateDeploymentError ($templateValidationResult ) {
353383 Write-Warning " Deployment template validation failed"
354384
@@ -357,14 +387,10 @@ function HandleTemplateDeploymentError($templateValidationResult) {
357387 return
358388 }
359389
360- # Retrieve one or more messages then decode the strings for readability (remove quote escapes, fix link readability, etc.)
361- $parsedMessage = ($templateValidationResult.Details.Message -join " $ ( [System.Environment ]::NewLine) $ ( [System.Environment ]::NewLine) " )
362- $parsedMessage = [System.Net.WebUtility ]::UrlDecode($parsedMessage )
363-
364390 Write-Warning " #####################################################"
365391 Write-Warning " ######### TEMPLATE VALIDATION ERROR DETAILS #########"
366392 Write-Warning " #####################################################"
367- Write-Host $parsedMessage
393+ Write-PSResourceManagerError $templateValidationResult 0 5
368394 Write-Warning " #####################################################"
369395}
370396
0 commit comments