Make the request to fail if an object is not found in the response body

hi colleagues,

i have a POST request which searchs for a particular object in the response body.Response body is an array like below

{
    "structureNodeList": [
        {
            "tenantId": "*",
            "retailStoreId": "101",
            "structureUniqueName": "*",
            "systemName": "*",
            "structureName": "*",
            "country": "*",
            "countryShort": "*"
        }
    ]
}

My request looks whether the retailSoreId=100 exists,in case retailSoreId=100 can be found,the next request will use it.My issue is ,if the retailSoreId=100 can not be found ,e.g like in the above response body instead of 100 the 101 is found,my request does not fail,it gives me the HTTPS status code 200.
I tried the following code

//check if structureUniqueName and retailStoreId for C4R Store has been found.If yes it will be deleted
var jsonData = JSON.parse(responseBody);
if (!jsonData.structureNodeList.find((a)=>a.retailStoreId===(store))) {
    console.log(`Store ${store} could not be deleted because it does not exsist in SM.`)
    throw "ERROR"  ;
    }else{
        pm.collectionVariables.set("completeStore", jsonData.structureNodeList.find((a)=>a.retailStoreId===(store)).structureUniqueName);
        pm.collectionVariables.set("retailStoreId", jsonData.structureNodeList.find((a)=>a.retailStoreId===(store)).retailStoreId);
        console.log(`StructureUniqueName ${pm.collectionVariables.get('completeStore')} has been found in SM .`)
        console.log(`Store ${pm.collectionVariables.get('retailStoreId')} is going to be deleted.`)
    }

It will make the call to stop and an error is thrown in console,but in the collection runner the call is not marked as failed

grafik

I think i need a test condition based on response body using pm.expect() but not sure how to do it if the body is an array and not a string.Any suggestion is appreciated

Hi @oalimerko

This is correct behaviour as the API call has been successfully made to the server and a response has been successfully passed back to you. The fact that the value stored in the JSON is not the value you are looking for doesn’t really make a difference to the 200 response code. It is simply an HTTP status code.

You could write in an expect assertion but this would not change the 200 response code, it would only pass or fail a test… like this;