Skip to content

Commit

Permalink
Improved error handling when compiling MOF files (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
raandree authored Mar 17, 2023
1 parent 3d31be5 commit 769ddad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion source/Scripts/CompileRootConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,21 @@ foreach ($module in $BuildInfo.'Sampler.DscPipeline'.DscCompositeResourceModules
}
}

Write-Host -Object ''
Write-Host -Object "Preloading available resources"

$availableResources = Get-DscResource

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)
Expand Down Expand Up @@ -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
}
}
}
10 changes: 7 additions & 3 deletions source/Tasks/CompileRootConfiguration.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 769ddad

Please sign in to comment.