psake error code management

843 views Asked by At

I have a task running a mercurial command that fails because mercurial is not returning 0 but 1.

When mercurial returns 1 does not mean it's an error but there is nothing to do. According to the documentation:

Returns 0 if push was successful, 1 if nothing to push.

How can I handle the return in my task so when the hg command returns 0 or 1 it won't break.

Here is the failing function

Function Invoke-HgBookmark([string]$bookmarkname)
{
    $repo = GetCredentials
    & $hg.file bookmark -f $bookmarkname
    Invoke-Expression "$($hg.file) push -B $bookmarkname $repo"
    if ($LASTEXITCODE -eq 0 -Or $LASTEXITCODE -eq 1)
    {
        return 0
    }
    else
    {
        return -1
    }
}

Psake is calling my function with this code:

try {
    $global:lastexitcode = 0                 
    & $cmd
   if ($lastexitcode -ne 0) {
        throw ("Exec: " + $errorMessage)
    }
    break
}
1

There are 1 answers

0
Keith Hill On BEST ANSWER

In the function, set $global:LastExitCode to the desired value before it exits. You have to use the global: prefix in order to modify the global variable. Otherwise PowerShell does a "copy-on-write" operation and modifies a variable with the same name that is local to the function.