Skip to content

Invoke-ServyCli: non-zero exit code caught by its own try/catch block #51

@Christophe-Rogiers

Description

@Christophe-Rogiers

Bug Description

In Invoke-ServyCli, the $LASTEXITCODE check is inside a try/catch block. When the CLI returns a non-zero exit code, the throw on line 212 is caught by its own catch block on line 215, resulting in a double-wrapped error message.

Actual Behavior

The current code:

try {
    & $script:ServyCliPath $finalArgs

    if ($LASTEXITCODE -ne 0) {
        throw "Servy CLI exited with code $LASTEXITCODE"  # line 212
    }
}
catch {
    throw "$($ErrorContext): $_"  # line 216 catches its own throw
}

When the CLI exits with code 1, the error becomes:

Failed to start service 'MyService': Servy CLI exited with code 1

This works, but the try/catch was intended for real exceptions (e.g. executable not found, access denied), not for exit code handling. The exit code throw is unintentionally caught by its own catch block.

Suggested Fix

Move the exit code check outside the try/catch so each error type is handled separately:

try {
    & $script:ServyCliPath $finalArgs
}
catch {
    throw "$($ErrorContext): $_"
}

if ($LASTEXITCODE -ne 0) {
    throw "$($ErrorContext): Servy CLI exited with code $LASTEXITCODE"
}

This way:

  • Exceptions (file not found, access denied) are caught by catch
  • Non-zero exit codes are handled explicitly after successful execution

Environment

  • PowerShell module: Servy.psm1
  • Affects: Invoke-ServyCli function (lines 193–218)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions