Saving an error code to a file if it exists in the response

I have some failing requests in a Gatling test which is ok. However I want to be able to look at the error codes after the test is ran.
Typically 1% of the calls fails with a HTTP 500 server error and throws this exception:


I want ot print “KJF-000134” to a file every time the response contains either “KJF” or “KJX”

How could I use a check to print only when the responses contains any of the two?



Please try to implement the below code,
In the API request write a check statement to save the Kode key using

1.write a check statement to save the entire JSON response
2 .check(jsonPath("$… kode ").find.saveAs(“C_kode”)) or regex which you find convenient [ for condition purpose ]

Then write a simple code to evaluate the condition check

.exec(session => {
val a1 = session(" C_kode").as[String]
// Write the string operation for a1 to replace everything after first three characters
println(a1) // to view the value
session.set(" C_kode ", C_kode )

.doIf(session => session(" C_kode").as[String].length() != 4)( // change the condition check as C_kode = KJF or KJX

.exec(session => {
val log = new File(“D:/File.json”)
val fw = new FileWriter(log, true)
val bw = new BufferedWriter(fw)
bw.write(session(“High_Priority_Size”).as[String] + “\n”) // use the session value in which you stored the entire Json response message

Aravind G