Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error reporting in psake

How do I get notified of any errors in any of the tasks in psake task list.?

In the notification handler, I would like to show a window notification that the task failed. But psake swallows the exception and writes it to the console.

Update: Here is the code in Psake build script that swallows the error

 # if we are running in a nested scope (i.e. running a psake script from a psake script) then we need to re-throw the exception
        # so that the parent script will fail otherwise the parent script will report a successful build 
        $inNestedScope = ($psake.context.count -gt 1)
        if ( $inNestedScope ) {
            throw $_
        } else {
            if (!$psake.run_by_psake_build_tester) {
                WriteColoredOutput $error_message -foregroundcolor Red
            }
        }
like image 786
Rockstart Avatar asked Jul 30 '26 16:07

Rockstart


1 Answers

This is how I'd handle this problem. First, somewhere before this exception swallowing code, make a new object called $errors, which is an ArrayList type (useful and quick for building a collection of messages).

$errors = New-Object System.Collections.ArrayList
# if we are running in a nested scope (i.e. running a psake script from a psake script) then we need to re-throw the exception
        # so that the parent script will fail otherwise the parent script will report a successful build 
        $inNestedScope = ($psake.context.count -gt 1)
        if ( $inNestedScope ) {
            throw $_
        } else {
            if (!$psake.run_by_psake_build_tester) {
                WriteColoredOutput $error_message -foregroundcolor Red
                $errors.Add($error[0])
            }
        }

        <#.the rest of your code...#>

       if ($errors.Count -ne 0){
        Write-Warning 'A number of errors were encountered during the processing of this task, please review them, below'
        $errors

       }

This is a very simple approach which will probably get the job done. We'll still allow an error to be written out to the screen, but also collect them all to display elsewhere in the script.

If this approach isn't what you're looking for, please let me know?

like image 180
FoxDeploy Avatar answered Aug 02 '26 13:08

FoxDeploy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!