Skip to content

fix: run catch blocks only for failed steps - #2760

Open
SanderSimson wants to merge 1 commit into
kyverno:mainfrom
SanderSimson:fix/1870-scope-catch-failure-state
Open

fix: run catch blocks only for failed steps#2760
SanderSimson wants to merge 1 commit into
kyverno:mainfrom
SanderSimson:fix/1870-scope-catch-failure-state

Conversation

@SanderSimson

Copy link
Copy Markdown

Explanation

This PR fixes an issue where the catch blocks from successful steps were executed after an earlier step failed. This polluted test output with diagnostic information from steps that had not failed. Catch execution is now scoped to the current step, while the overall test still remains failed when an operation fails.

Related issue

Fixes #1870

Proposed Changes

  • Track failure state locally for each step in runStep.
  • Execute a step's catch block only when that step's try block fails.
  • Preserve the existing overall test failure and continueOnError behavior.
  • Add a unit test covering a failed step followed by a successful step.

Create the following test/chainsaw-test.yaml and run it with ./chainsaw test --no-cluster test to confirm behavior:

apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
  name: chainsaw-test-catch-continue-on-error
spec:
  steps:
    - name: Failed step should run its catch block
      try:
        - continueOnError: true
          script:
            content: |
              # Fail deliberately so the first step's catch block is expected to run.
              exit 1
      catch:
        - script:
            content: |
              # Emit a distinctive marker for the catch block belonging to the failed step.
              echo 'chainsaw-test: catch from failed step'

    - name: Successful step A should not run its catch block
      try:
        - continueOnError: true
          script:
            content: |
              # Succeed deliberately so this step's catch block is expected to stay silent.
              exit 0
      catch:
        - script:
            content: |
              # Emit a marker that should never appear because the step succeeds.
              echo 'chainsaw-test: catch from successful step A'

    - name: Successful step B should not run its catch block
      try:
        - continueOnError: true
          script:
            content: |
              # Succeed deliberately so this step's catch block is expected to stay silent.
              exit 0
      catch:
        - script:
            content: |
              # Emit a marker that should never appear because the step succeeds.
              echo 'chainsaw-test: catch from successful step B'

Checklist

  • I have read the contributing guidelines.
  • I have read the PR documentation guide and followed the process including adding proof manifests to this PR.
  • This is a bug fix and I have added unit tests that prove my fix is effective.

Further Comments

@SanderSimson
SanderSimson requested a review from a team as a code owner August 3, 2026 06:32
@SanderSimson
SanderSimson force-pushed the fix/1870-scope-catch-failure-state branch from 39a1f83 to 91a1074 Compare August 3, 2026 06:38
Signed-off-by: Sander Simson <sander.simson@wise.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant