Dynamic values error handling

Hi, I have requests where data is substituted from previous response (dynamic value).
If previous request fails then next request will also,
How to handle this case to have less errors in results.


Hello again! :slight_smile:

As you’ve correctly pointed out, a request failure will by default not cause the VU to skip the rest of the iteration or even stop the VU altogether. I agree that it would be useful to be able to determine (globally) what should happen when a VU receives an unexpected request. Unfortunately, in some situations, requests may be expected to receive a 4xx-5xx response, despite those being in the error range. I believe this is why we don’t make any assumptions about what is considered a success/failure.

What would be useful here is if we were to automatically add code to check that received response status codes match what was encountered in the original recording (assuming the script was generated from a recording, of course).

For now, you’ll need to add code in the places where you expect the failures to happen. Something like this should do the trick:

import { fail } from "k6";
import http from "k6/http";

export default function() {
  const response = http.post("https://httpbin.org/status/500", { foo: "bar" });

  if (response.status === 500) { // adjust as necessary
    fail("Request failed. Response was: " + response.body); // skips to the next iteration

  console.log("this will only appear if there was no failure");