How to capture response error and add to html report

Hi, I have a script and need to capture the response error for failed api requests to print on html report to show the issue of particular api request but I’m not able to capture. could you please any one suggest where I’m doing the mistake.

few requests getting TypeError: Cannot read property ‘json’ of undefined or TypeError: Cannot read property 0 of undefined error.

Please find my script

import http from "k6/http";
import { check, sleep } from "k6";
import { htmlReport } from "";
import { textSummary } from "";
import papaparse from "";
import { SharedArray } from "k6/data";
var dateFormat = new Date();
var getDateFormat = dateFormat.toString().substring(4,25).replace(" ","_").replace(" ","_").replace(" ","_").replace(":","_").replace(":","_").trim();

const loginCredentials = new SharedArray('users', function () {
  return papaparse.parse(open('./testData/login_credentials.csv'), { header: true }).data;

const aTestData = JSON.parse(open("./testData/sbt_testdata.json"));

export const options = {
  //stages: [{ duration: "10m", target: 1000}],
  stages: [{ duration: "1m", target: 100}],
  //stages: [{ duration: "1s", target: 1}],

export default function () {
  //console.log("getDateFormat "+getDateFormat);
  const baseURL = "";
  const xapikey =
  let responseError = ""; 
    let params = {
        timeout: '120s'

    let newXsrfToken = "";
    let XSRFTOKEN = "";
    let cookieData = "";
    var sleepValue = 0.6;

  const payLoad = {
    clientId: randomUser.companyID,
    userId: randomUser.username,
    password: randomUser.pwd,

  sleep(Math.random() * sleepValue);
  let response_about = http.get(baseURL + "about");
  sleep(Math.random() * sleepValue);
  console.log("response_about "+response_about.status);

  if(response_about.status == 200){
    console.log("### inside condition "+response_about.status);
    cookieData = response_about.cookies;
    XSRFTOKEN = cookieData["XSRF-TOKEN"][0].value;

  if(XSRFTOKEN != ""){
    //sleep(Math.random() * 30);
    //console.log("response_about body "+response_about.body);
    console.log("XSRFTOKEN ### "+cookieData["XSRF-TOKEN"][0].value);
    console.log("response_about status "+ response_about.status);
    throw 'XSRFTOKEN ' + XSRFTOKEN + 'is not generated.';

  responseError = response_about.error.json;
  console.log("responseError "+responseError);


Hi @bikshamar,
can you clarify where you are getting the error? Is it related to the following line?

  responseError = response_about.error.json;

As you can see from the http.Response documentation error is a string and not an object, so if your attempt is to store it as a string text then you can do it directly.

Let me know if that helps.

Thank you for your response. I’m trying to capture the error if any api request is failed so that i can show to the stake holder about where the test is failing and reason for failure. showing few requests as failed in the html report but there is no api error or anything. it is hard to find why it is failing

even i added the condition to check but still not showing the error for failed request.

responseError = response_about.error;
console.log("responseError "+responseError);

if(responseError != “”){
check(responseError, {
['Launching URL API Error Message : '+responseError+" Response URL "+response_about.url] : 200,

Getting the following error in the log if i use response_about.error.json;
{“executor”:“ramping-vus”,“level”:“error”,“msg”:"TypeError: Cannot read property ‘json’ of undefined\n\tat

I followed below post and it helped to capture error messages. I’m able to capture the response errors i html report.