cancel
Showing results for 
Search instead for 
Did you mean: 

CAP - Read HTTP Status from External Service

VoJu
Participant
0 Kudos

Hello together,

I followed the following approach to consume an external service:

https://cap.cloud.sap/docs/guides/using-services#execute-queries

But how can I read the HTTP Status, to see if the call was successful?

Thanks a lot and best regards,

Julian

robinjayasinghe
Employee
Employee
0 Kudos

If the call returns a result I would say the call was successful. In case an error/exception is thrown you can inspect the error in a debugger and see if it contains a HTTP code. With CAP Java, the code should throw a ServiceException which has a getErrorStatus() method.

HTH,

Robin

Accepted Solutions (1)

Accepted Solutions (1)

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi julian_vogel,

In addition to what my colleague robinjayasinghe said above, please read the following:

https://cap.cloud.sap/docs/node.js/best-practices#availability-checks

On either case (Java or NodeJS), the execution on a connection object will result in either having data or it will be empty.

In case it is empty, it would probably be caused by a service call error. In that case, errors usually have any meaningful information to really help the end-user. Therefore, it is usually irrelevant to be displayed at all.

If you really need it for debugging purposes, than you could use a simple try...catch (either on Java or NodeJS) and place your run method calls inside it. Check the following NodeJS example below:

module.exports.getBPAddress = async zlimit => {
	const LOG = cds.log('getBPAddress', { level: 'warn' });
	const bupa = await cds.connect.to('API_BUSINESS_PARTNER');
	const { A_BusinessPartner } = bupa.entities;
	
	try {
		const result = await bupa.run(SELECT.from(A_BusinessPartner, bp => {
			bp('BusinessPartner'),
			bp.to_BusinessPartnerAddress(addresses => {
			  addresses('*')
			})
		  }).limit(zlimit));
  } catch (error) {
    LOG.info("API returned error: ", error);
  }
  
};

Best regards,
Ivan

Answers (0)