cancel
Showing results for 
Search instead for 
Did you mean: 

Catche function did nt called in Sapui5 application

madhava_munagala
Explorer
0 Kudos

Hi Team,

Let API respponse = 503 status // declared globally

function makeApiCall(Product, Plant) {

var baseApiUrl = "XXXXXXProductAPI";

var filterParams = "$filter=Product eq '" + Product +

"' and Plant eq '" + Plant +

"' &$select=Product,Plant,SerialNumberProfile_1,ProductName";

var sApiUrl = baseApiUrl + "?" + filterParams;

return new Promise(function (resolve, reject) {

jQuery.ajax({

url: sApiUrl,

type: "GET",

contentType: "application/json",

dataType: "json",

success: function (oResponse) {

if (oResponse.d.results.length > 0) {

resolve(oResponse);

} else {

resolve(null);

}

},

error: function (jqXHR, textStatus, errorThrown) {

SPResponsetext = jqXHR.responseText;

try {

if (APIResponsetext === SPResponsetext) {

reject(new Error("Session out reload the APP"));

// MessageToast.show("Session out reload the APP");

} else {

// If the condition is not met, reject with the error message

reject(new Error(JSON.parse(jqXHR.responseText).error.message.value));

}

} catch (error) {

// Log the error and reject with the original response text

reject(new Error(jqXHR.responseText));

// MessageToast.show("Error in Serialised API try block:"+ jqXHR.responseText);

}

}

});

});

}

// Create an array to store promises for each API call

var aPromises = [];

async function f2() {

try {

for (var r = 0; r < rowlength; r++) {

var Product = oTable.getSelectedItems()[r].getAggregation("cells")[1].getValue();

var TWPlant = oTable.getSelectedItems()[r].getAggregation("cells")[11].getValue();

const promise = await makeApiCall(Product, TWPlant);

aPromises.push(promise);

}

Promise.all(aPromises)

.then(function (serialisedarrpro) {

// All API calls are successful

console.log("All Serialised API calls completed successfully:", serialisedarrpro);

var serialisedarr = serialisedarrpro.filter(function (element) {

return element !== null;

});

that.SerialProfileValidation(serialisedarr);

})

.catch(function (oError) {

// Handle errors

console.log("API calls failed:", oError);

MessageToast.show(oError);

});

} catch (err) {

console.log(err);

MessageToast.show(err);

}

}

f2();

Step 1 - const promise = await makeApiCall(Product, TWPlant); Calling loop item to this function.

Step 2 - if it is successful. it will go to success block. assume 5 calls are successfull.

Step 3 - Let assume, my 6th row item failed. If condition satisfies in error block - if (APIResponsetext === SPResponsetext). im checking if API status === 503 status here.

Step4 - once step 3 done,

My API is failing with status 503 at some point of time I'm able to found the error in Last Catch block(catch (err) {

console.log(err);MessageToast.show(err);}) in f2 function, why error is not capturing catch followed by try block inside the F2 function(try block follows by promise.all/then/catch). 3. Could you please suggest me why my catch is not triggered from try block follows by promise.all/then/catch.4. Could you please help me/any suggestions for fixing the code.

Accepted Solutions (0)

Answers (1)

Answers (1)

Fjaoos
Participant
0 Kudos

Hi, may I suggest showing this code formatted? No on will be able to read this properly

madhava_munagala
Explorer
0 Kudos

Hi nils.janssen001 ,

I have mentioned steps now. Please check and let me know

Thanks,

Madhava