# How to fail a single stage through plugin?

**URL:** <https://community.jenkins.io/t/how-to-fail-a-single-stage-through-plugin/12951>\
**Category:** Ask a question\
**Tags:** question\
**Created:** [March 1, 2024, 6:25am UTC](https://community.jenkins.io/t/how-to-fail-a-single-stage-through-plugin/12951 "2024-03-01T06:25:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaman-akib](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/zaman-akib/32/7053_2.png) [@zaman-akib](https://community.jenkins.io/u/zaman-akib)\
**Post date:** [March 1, 2024, 6:25am UTC](https://community.jenkins.io/t/how-to-fail-a-single-stage-through-plugin/12951/1 "2024-03-01T06:25:11Z")

</div>

Currently we are developing a Jenkins plugin as a Step. Now in case of errors we want to fail the single stage from where our step is called in Jenkinsfile. We’ve got a way to mark the whole build as failed but we want the next stages should execute but our stage should be failed in case of error. How can we achieve that through our plugin?

Note- We are using Jenkins version 2.401.3

---

<div class="post-metadata">

**Author:** ![poddingue](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/poddingue/32/985_2.png) [@poddingue](https://community.jenkins.io/u/poddingue)\
**Post date:** [March 4, 2024, 9:23am UTC](https://community.jenkins.io/t/how-to-fail-a-single-stage-through-plugin/12951/2 "2024-03-04T09:23:09Z")

</div>

In Jenkins, if a stage fails, the default behavior is to stop the entire pipeline. However, I think you could change this behavior by using the `catchError` or `catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')` step in your Jenkinsfile.  
This should allow the pipeline to continue even if a stage fails.

Here is an untested example of how you could maybe use `catchError` in your `Jenkinsfile`:

```auto
pipeline {
    agent any
    stages {
        stage('Stage 1') {
            steps {
                // Your steps here
            }
        }
        stage('Stage 2') {
            steps {
                script {
                    catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                        // Call your plugin here
                    }
                }
            }
        }
        stage('Stage 3') {
            steps {
                // Your steps here
            }
        }
    }
}

```

In this example, if your plugin (called in Stage 2) fails, the stage will be marked as failed but the pipeline will continue to Stage 3.  
Please note that the `catchError` step is part of the [Pipeline Basic Steps plugin](https://plugins.jenkins.io/workflow-basic-steps/), which should be installed and available in most Jenkins environments (93% at the time of writing).  
As for marking the stage as failed from within your plugin, you would need to throw an exception when an error occurs in your plugin. This will cause the `catchError` step to mark the stage as failed.

Here’s a simple example in Java:

```auto
public class YourPluginStep extends Step {
    @Override
    public StepExecution start(StepContext context) throws Exception {
        // Your plugin code here

        if (/* error condition */) {
            throw new AbortException("Error message");
        }

        return new YourPluginStepExecution(context);
    }
}

```

In this example, if the error condition is met, an `AbortException` is thrown, which will cause the `catchError` step to mark the stage as failed.

Hope this helps. 🤷

---

<div class="post-metadata">

**Author:** ![zaman-akib](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/zaman-akib/32/7053_2.png) [@zaman-akib](https://community.jenkins.io/u/zaman-akib)\
**Post date:** [March 4, 2024, 9:58am UTC](https://community.jenkins.io/t/how-to-fail-a-single-stage-through-plugin/12951/3 "2024-03-04T09:58:33Z")

</div>

Thanks @poddingue. Appreciate it.

---

<div class="post-metadata">

**Author:** ![zaman-akib](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/zaman-akib/32/7053_2.png) [@zaman-akib](https://community.jenkins.io/u/zaman-akib)\
**Post date:** [July 26, 2024, 6:46am UTC](https://community.jenkins.io/t/how-to-fail-a-single-stage-through-plugin/12951/4 "2024-07-26T06:46:00Z")

</div>

I went through the **unstable** step implementation in GitHub - [workflow-basic-steps-plugin/src/main/java/org/jenkinsci/plugins/workflow/steps/UnstableStep.java at 1341de542ab6001ca7161d0a8543aa8195b8a6b8 · jenkinsci/workflow-basic-steps-plugin · GitHub](https://github.com/jenkinsci/workflow-basic-steps-plugin/blob/1341de542ab6001ca7161d0a8543aa8195b8a6b8/src/main/java/org/jenkinsci/plugins/workflow/steps/UnstableStep.java)  
and found out that, there is a way to fail a single step from plugin and continue to execute the next steps -

```auto
@Override
protected Integer run() throws PluginExceptionHandler {
    int exitCode = executeTask();
    if (exitCode != 0) {
        getContext().get(FlowNode.class)
                    .addOrReplaceAction(
                         new WarningAction(Result.FAILURE)
                         .withMessage("Your Message")
                     );
    }

}

```

With this, the expected behavior can be achieved. This is only applicable when you want to fail your step based on some condition from plugin and want the next steps to continue.
