Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing without throwing from within job #68

Open
God-damnit-all opened this issue Jul 31, 2023 · 0 comments
Open

Failing without throwing from within job #68

God-damnit-all opened this issue Jul 31, 2023 · 0 comments

Comments

@God-damnit-all
Copy link

I am wanting to abort a ThreadJob early if $LASTEXITCODE doesn't equal 0. Throwing accomplishes that, but I'm wanting the return value to only be the stderr output of the native application.

Here's what I would like if exit 1 hypothetically set the job's State property to "Failed":

$jobObject = Start-ThreadJob {
    curl --silent --show-error this_will_cause_an_error
    if ($LASTEXITCODE) {
        exit 1
    }
}
$null = Wait-Job $jobObject
$jobData = Receive-Job $jobObject 2>&1
if ($jobObject.State -eq 'Failed') {
    throw $jobData
}

And here is my current, ugly workaround:

$jobObject = Start-ThreadJob {
    $err = $(
        $out = curl --silent --show-error this_will_cause_an_error
    ) 2>&1
    if ($LASTEXITCODE) {
        throw $err
    } else {
        Write-Output $out
    }
}
$null = Wait-Job $jobObject
$jobData = Receive-Job $jobObject 2>&1
if ($jobObject.State -eq 'Failed') {
    throw $jobData
}

My problem with this is that the redirection wrapping you have to resort to is very kludgy, with $err being the outermost variable being set and the proper output set within a subexpression. I'm hoping for an alternative.

As mentioned before, exit 1 would be great, though I'm not sure how you'd feel about that producing a different State result from it being used in a BackgroundJob, since it doesn't work there either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant