From 769ddad89be69d74d4ef776636f3578926bfabeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raimund=20Andr=C3=A9e=20=5BMSFT=5D?= Date: Fri, 17 Mar 2023 13:18:08 +0100 Subject: [PATCH] Improved error handling when compiling MOF files (#23) --- CHANGELOG.md | 1 + source/Scripts/CompileRootConfiguration.ps1 | 12 +++++++++++- source/Tasks/CompileRootConfiguration.build.ps1 | 10 +++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e92f923..8bd86c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Increase build speed of root configuration by only importing required Composites/Resources - Added ''UseEnvironment'' parameter to cater for RSOP for identical node names in different environments - Adding Home.md to wikiSource and correct casing. +- Improved error handling when compiling MOF files. ### Fixed diff --git a/source/Scripts/CompileRootConfiguration.ps1 b/source/Scripts/CompileRootConfiguration.ps1 index 69f0817..e9eb8c7 100644 --- a/source/Scripts/CompileRootConfiguration.ps1 +++ b/source/Scripts/CompileRootConfiguration.ps1 @@ -51,7 +51,9 @@ foreach ($module in $BuildInfo.'Sampler.DscPipeline'.DscCompositeResourceModules } } +Write-Host -Object '' Write-Host -Object "Preloading available resources" + $availableResources = Get-DscResource Write-Host -Object '' @@ -59,6 +61,11 @@ Write-Host -Object '' $configData = @{} $configData.Datum = $ConfigurationData.Datum +if (-not $rsopCache) +{ + Write-Error -Message "No RSOP cache found. The task 'CompileDatumRsop' must be run before this task." +} + foreach ($node in $rsopCache.GetEnumerator()) { $importStatements = foreach ($configurationItem in $node.Value.Configurations) @@ -88,6 +95,9 @@ foreach ($node in $rsopCache.GetEnumerator()) { Write-Host -Object "Error occured during compilation of node '$($node.NodeName)' : $($_.Exception.Message)" -ForegroundColor Red $relevantErrors = $Error | Where-Object Exception -IsNot [System.Management.Automation.ItemNotFoundException] - Write-Host -Object ($relevantErrors[0..2] | Out-String) -ForegroundColor Red + foreach ($relevantError in ($relevantErrors | Select-Object -First 3)) + { + Write-Error -ErrorRecord $relevantError + } } } diff --git a/source/Tasks/CompileRootConfiguration.build.ps1 b/source/Tasks/CompileRootConfiguration.build.ps1 index e7a184b..4f2d2f2 100644 --- a/source/Tasks/CompileRootConfiguration.build.ps1 +++ b/source/Tasks/CompileRootConfiguration.build.ps1 @@ -13,7 +13,7 @@ param [Parameter()] # Base directory of all output (default to 'output') [System.String] - $OutputDirectory = (property OutputDirectory (Join-Path -Path $BuildRoot -ChildPath output)), + $OutputDirectory = (property OutputDirectory (Join-Path -Path $BuildRoot -ChildPath output)), [Parameter()] [string] @@ -92,12 +92,16 @@ task CompileRootConfiguration { } catch { - Write-Build Red "ERROR OCCURED DURING COMPILATION: $($_.Exception.Message)" + Write-Build Red 'Error(s) occured during the compilation. Details will be shown below' + $relevantErrors = $Error | Where-Object -FilterScript { $_.Exception -isnot [System.Management.Automation.ItemNotFoundException] } - $relevantErrors[0..2] | Out-String | ForEach-Object { Write-Warning -Message $_ } + foreach ($relevantError in ($relevantErrors | Select-Object -First 3)) + { + Write-Error -ErrorRecord $relevantError + } } finally {