Attempting to fail a pipeline conditionally from within a node.js script called from bash
Asked by Andy Rouse:

As part of an automated pipeline, I'd like to run some data quality checks on the files being merged. To do this, I'm running a node.js script. If data quality constraints aren't met, I'd like to fail the pipeline with a message.

I've tried every suggested option in the following stack exchange article, but none are working: [How do I fail a script running in Bitbucket Pipelines?](https://stackoverflow.com/questions/46039308/how-do-i-fail-a-script-running-in-bitbucket-pipelines#:~:text=on%20this%20post.-,Bb%20Pipelines%20fail%20when%20a%20command%20exits%20with%20a%20non,the%20code%20is%20not%200.)

I can see in the output from the Pipeline that the exception is being thrown:

![Screenshot 2023-03-02 091314.png](/t5/image/serverpage/image-id/243325i09800776B4B48DDD/image-size/large?v=v2&px=999 "Screenshot 2023-03-02 091314.png")

However, the pipeline is just carrying on to the next line in the bash script, rather than failing with the message that has been set.


Answers:
author: Erez Maadani
body: Hey [@Andy Rouse](/t5/user/viewprofilepage/user-id/5068265)

If I understand correctly, you have the pipeline script which triggers a node.js script.

Usually, shell scripts tend to ignore errors and use only the last command result code as the whole script result code. So if you have a script which looks like that:

func1 //This complete successfully

func2 //This fails

func3 //This complete successfully

Then the result of the whole script is successful.

In such cases you have two options:

1. Check the result of each func and exit if the result code is not 0.
2. Try and add flags which control the script behavior. If you are using bash or shell scripts: "set -e" and/or "set -o pipefail" will help you to stop the execution in case of an error in one of the func calls.

comments: author: Andy Rouse
body: Hey [@Erez Maadani](/t5/user/viewprofilepage/user-id/5018344) - Thanks for the reply. The script I'm calling is Node.js.

For clarity - Are you saying there's nothing i can do directly in the Node.js script to cause the pipeline to to fail? That's useful to know.

Additionally, I'm inspecting the error code from the node.js (which I'm having throw an error) and the exit code is still 0. Is there an established way to set the exit code from node.js?

Is there any way to inspect the message in the exception being thrown by the node.js script? It's not 100% necessary for this specific script, as the action is simple and it can only really fail for one reason. However, it would be good to know if there's a general model for inspecting an error code/message from a node.js script in a pipeline.


author: Erez Maadani
body: Hey [@Andy Rouse](/t5/user/viewprofilepage/user-id/5068265)

I'm saying that even if your Node.js fails, I believe you would still need to change your pipeline **bash** script.

To exit your Node.js script with a return code which is not 0, try (in your Node.js code) catching the error and then calling:

    process.exit(1);

See here: <https://nodejs.org/api/process.html#processexitcode>


author: Andy Rouse
body: [@Erez Maadani](/t5/user/viewprofilepage/user-id/5018344) - Thank you for your help so far. Unfortunately - that's not working.

Whatever I'm doing (I've tried multiple approaches), the exit code is always 0. I've thrown an exception, caught the exception and then used process.exit, the result is still a zero exit code.

My latest version is as follows

![Screenshot 2023-03-05 182739.png](https://community.atlassian.com/t5/image/serverpage/image-id/243841iB589D9317394763D/image-size/large?v=v2&px=999 "Screenshot 2023-03-05 182739.png")

![Screenshot 2023-03-05 183026.png](https://community.atlassian.com/t5/image/serverpage/image-id/243842i1D52C335897DC8DF/image-size/large?v=v2&px=999 "Screenshot 2023-03-05 183026.png")

![Screenshot 2023-03-05 183231.png](https://community.atlassian.com/t5/image/serverpage/image-id/243843i89E5F703F5B0EBC4/image-size/large?v=v2&px=999 "Screenshot 2023-03-05 183231.png")

![Screenshot 2023-03-05 183334.png](https://community.atlassian.com/t5/image/serverpage/image-id/243844i053644ABAD62349F/image-size/large?v=v2&px=999 "Screenshot 2023-03-05 183334.png")

I've tried multiple variations of the code, but whatever happens, I get a zero exit code. As you can see, I've attempted to call process.exit() within the logic in the first screenshot (I commented out the "throw" line), I've also tried making the process async and not async, in case that makes a difference. The differences tend to be that the custom thrown exception will or won't be shown in the output from the pipeline, but the exit code is always the same.


author: Erez Maadani
body: [@Andy Rouse](/t5/user/viewprofilepage/user-id/5068265)

As mentioned before, the command \`echo $?\` shows the exit code of the previous command, which is \`echo exit code\`...

Try this in your Node.js code:

```
if (dupeCustomLabels.size >0){
  console.log(...)
  process.exit(1)
}
```

And in your pipeline script:

```
node yourScript.js
rc=$?
echo $rc
[[ "$rc" -ne "0" ]] && exit(1) #this will fail the pipeline if rc is not zero   
```


author: Andy Rouse
body: Hey [@Erez Maadani](/t5/user/viewprofilepage/user-id/5018344) - I got this working. There were a couple of things wrong with my code, a few of which I was able to resolve with your help:

* The "echo exit code" row was preventing me from capturing the exit code from the JS
* I hadn't properly imported the *process* module, so my process.on() method wasn't firing properly

Once I'd resolved those two, I no longer had to use process.exit(), as throwing the exception within the process.on() method implicitly set the exit code to 1 (and I also see the exception method in the log)

The if statement in your response was helpful, didn't work for me. I think it had too many speech marks in it, as it caused an error at runtime. However, it did point me in the right direction and I was able to adapt it slightly to make it work for me:

![Screenshot 2023-03-07 182650.png](https://community.atlassian.com/t5/image/serverpage/image-id/244334i59E84325D14605F1/image-size/large?v=v2&px=999 "Screenshot 2023-03-07 182650.png")

With these in place, I was now able to throw an exception, so that the Pipeline will fail and the error message is available in the log for the pipeline. Thank you.


Tags: bitbucket-cloud

cloud

exception

node