hi Team,
How can I design a script to stop executing the preceding requests, if the dependent API fails?
Kindly help me.
Sorry, I am confused by the question, the word “preceding” specifically I think you mean “following”? If so, then you can just return
from the function early or throw
an error, that will prevent k6 from continuing the iteration. Something like this:
import http from 'k6/http';
export const options = {
iterations: 5,
};
export default function () {
// 30% chance to hit an URL with status code 500
let url = 'https://httpbin.test.k6.io/status/' + (Math.random() < 0.3 ? '500' : '200')
let resp = http.get(url)
if (resp.status != 200) {
throw new Error(`Got unexpected status ${resp.status}`)
// or use return, if you don't want to throw an exception
}
console.log('Continuing to make another dependent request...')
http.get('https://httpbin.test.k6.io/anything')
}
You might also want to add a custom metric to track such errors, see: Metrics
Hi, Ned
Sorry for the confusion. Yes, I mean following. I will try with the above function. Thanks for the reply
Hello, Ned it worked. Thank you