I think it is totally wrong to close all code in Set() to try-catch block and then output exception message in verbose stream. Of course there are reasons why to add part of your code that can fail to try-catch and then output the exception in verbose stream. But if you put everything in Set() to try-catch then you have no information that Set() failed (nothing in event log).
I prefer to put
$ErrorActionPreference = 'Stop'
at the beginning of all DSC methods (Get(), Test(), Set()) and then do a proper error handling using try-catch blocks.
It's definitely bad to have the errors only going out through the Verbose stream. That will make the LCM think that the Set method was successful, and any other resources that might be using a DependsOn declaration to this resource would start to be executed (instead of having the LCM not process those due to the upstream failure.)
I'm also not sure why they bothered having SupportsShouldProcess and $PSCmdlet.ShouldProcess() calls in the Set-TargetResource functions of some of the DSC resource kit. The LCM never calls that function with -Confirm or -WhatIf anyway, so there doesn't seem to be any point.
The topic ‘Custom DSC Resources - Classes - Error handling’ is closed to new replies.