Custom DSC Resources - Classes - Error handling

Hello,
I would like to discuss with you error handling in custom DSC resources (mainly for classes). I have seen several times similar code:

[void] Set()
{
    try {
        # Do something
    } catch {
        Write-Verbose -Message $_.Exception.Message
    }
}

For example here: xTimeZoneClassSingleInstance/xTimeZone.psm1 at master · TravisEz13/xTimeZoneClassSingleInstance · GitHub
Btw. the code in example will not work because there is (for any reason) $PSCmdlet.ShouldProcess.

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.

What do you think?

Thank you and have and have a nice day :wink:
Rudolf

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.