Fail( [err] ) the command stopped working

i had a few of fail( [err] ) in my script.
today they stopped working

import http from 'k6/http';
import { check, fail } from 'k6';

export default function () {
  const res = http.get('https://k6.io');
  if (
    !check(res, {
      'status code MUST be 200': (res) => res.status == 99999,
    })
  ) {
    fail('status code was *not* 99999');
  }
}

getting a following messages on console

Hi @HardBoiledEG

I tried a similar script (I just had to change the status code as 99999 is not accepted in HTTP1.1):

import http from 'k6/http';
import { check, fail } from 'k6';

export default function () {
    const res = http.get('https://httpbin.test.k6.io/status/999');
    //const res = http.get('https://k6.io/');
    if (
        !check(res, {
            'status code MUST be 200': (r) => r.status === 999,
        })
    ) {
        fail('status code was *not* 999');
    }
}

This works well for me.

I find it a bit confusing that the check name is 'status code MUST be 200', while you check for 99999. However, as in my example, if the endpoint is returning a 999 (or 99999 for you), it should work. I would just change the check name to make the script clearer, 'status code MUST be 999':

import http from 'k6/http';
import { check, fail } from 'k6';

export default function () {
    const res = http.get('https://httpbin.test.k6.io/status/999');
    //const res = http.get('https://k6.io/');
    if (
        !check(res, {
            'status code MUST be 999': (r) => r.status === 999,
        })
    ) {
        fail('status code was *not* 999');
    }
}

That said, when you say this stopped working, did you check if anything change in your SUT (System Under Test)?

I would suspect it’s not returning the status code you are checking for (be it 99999 - your script - or 9999 as your screenshot shows). I would check this with a curl command, similar to:

curl -X GET "https://httpbin.test.k6.io/status/999" -v

And make sure it’s returning the status code you are checking for in the test script. In my case it’s indeed HTTP/1.1 999 UNKNOWN:

< HTTP/1.1 999 UNKNOWN
< Date: Fri, 10 Mar 2023 14:01:31 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 0
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true

Let me know if that helps.

Cheers!

thanks for taking the time to answer.
Looks like, I did not explain the issue properly.
I need to fail the test if the Status !== 200
see following code

import http from 'k6/http';
import { check, fail } from 'k6';

export default function () {
  let host_name;
  host_name = 'https://k6.io'; //STATUS 200 OK
  host_name = 'https://k6.io+++'; //STATUS is not 200
  const res = http.get(host_name);
  let api_code = 200;
  if (
    !check(res, {
      [`status code MUST be ${api_code}`]: (res) => res.status == api_code,
    })
  ) {
    fail(`status code was *not* ${api_code} - [${res.error}/ ${res.error_code}]`);
  }
}```

Hi @HardBoiledEG

This should be pretty straight forward from a check in the k6 documentation:

import http from 'k6/http';
import { check, fail } from 'k6';

export default function () {
    //const res = http.get('https://httpbin.test.k6.io/status/403');
    const res = http.get('https://httpbin.test.k6.io/status/200');
    if (
        !check(res, {
            'status code MUST be 200': (r) => r.status === 200,
        })
    ) {
        fail('status code was *not* 200');
    }
}

This works:

If you execute with the URL that returns, e.g., a 403, const res = http.get('https://httpbin.test.k6.io/status/403, it fails.

Depending on your needs you can even mix checks and thresholds: Thresholds.

import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
    vus: 50,
    duration: '10s',
    thresholds: {
        // the rate of successful checks should be higher than 90%
        checks: ['rate>0.9'],
    },
};


export default function () {
    const res = http.get('https://httpbin.test.k6.io/status/200');
    check(res, {
        'status is 200': (r) => r.status == 200,

    });
    sleep(1);
}

I hope this helps.

Cheers!

@eyeveebe Thanks again.
However, u are getting the same issue as I am. A compilation error
ERRO[0000] GoError: status code was *not* 200 running at go.k6.io/k6/js/modules/k6.(*K6).Fail-fm (native)
As a result, the test is aborted.
My understanding, the fail shouldaAbord the current script iteration if a check fails fail the C

consider the following code

import http from 'k6/http';
import { check, fail } from 'k6';

export function make_call(end_point){
  const res = http.get(end_point);
  if (
      !check(res, {
          'status code MUST be 200': (r) => r.status === 200,
      })
  ) {
      fail('status code was *not* 200');
  }

}

export default function () {
  let end_point;
  //end_point = http.get('https://httpbin.test.k6.io/status/200');
  end_point = http.get('https://httpbin.test.k6.io/status/403');
  make_call(end_point)
  console.log(`DONE`);// Last command
}

Then the make_call fails, the last command in default function is not executed

The script should not be aborded on fail

Hi @HardBoiledEG

In the example I was just executing an iteration, and it stopped. However, you can define a script with 1 VU, 10 iterations:

import http from 'k6/http';
import { check, fail } from 'k6';

export const options = {
    vus: 1,
    iterations: 10,
}
export default function () {
    const res = http.get('https://httpbin.test.k6.io/status/404');
    if (
        !check(res, {
            'status code MUST be 200': (res) => res.status == 200,
        })
    ) {
        fail('status code was *not* 200');
    }
}

And you will see the 10 errors.

Each iteration fails.

I hope this helps clarify the fail( [err] ) behavior.

1 Like

I am so mad at meself. Just added following to a Calling function

try{
  }
  catch{
  }

and everything started making sense and working
Thanks, again.

import http from 'k6/http';
import { check, fail } from 'k6';

export function make_call(end_point){
  const res = http.get(end_point);
  if (
      !check(res, {
          'status code MUST be 200': (r) => r.status === 200,
      })
  ) {
      fail('status code was *not* 200');
  }

}

export default function () {
  let end_point;
  //end_point = http.get('https://httpbin.test.k6.io/status/200');
  end_point = http.get('https://httpbin.test.k6.io/status/403');
 try{
     make_call(end_point)
     }
 catch(error){
     console.log(`${error}`);
     }
  console.log(`DONE`);// Last command
}
1 Like