VMware Cloud Community
craigso
Enthusiast
Enthusiast
Jump to solution

Error handling for external REST API call

Hello!

Thank you for taking the time to read this post. I'm looking for some assistance with error handling of an external API call. For example, if the host doesn't respond, wait 10 seconds and retry. The part I'm hung up on at the moment is trying to retrieve the error or status so that I can take actions on that. Here is how I am currently making the call to the external API:

// Create Dynamic REST Host

var restHost = RESTHostManager.createHost("DynamicRequest");

var apiGatewayHost = RESTHostManager.createTransientHostFrom(restHost);

apiGatewayHost.url = "https://api.host.com/service/";

request = apiGatewayHost.createRequest("GET", "host?subnet=10.128.191.160/28");

request.setHeader("Accept","application/xml");

request.setHeader("Content-Type","application/x-www-form-urlencoded");

request.setHeader('X-USERNAME', 'USERNAME');

request.setHeader('X-PASSWORD', 'PASSWORD');

request.setHeader('Ocp-Apim-Subscription-Key', 'key-XXXXXXXXXXXXX');

response = request.execute();

// Take API response(XML) and convert it to JSON

jsonResponse = JSON.parse(RESTUtils.xml2json(response.contentAsString));

Is the status returned anywhere? and if so, I'd like to use it to catch the error and retry the call once more before throwing and error.

Reply
0 Kudos
1 Solution

Accepted Solutions
eoinbyrne
Expert
Expert
Jump to solution

Just to build on what iiliev​ has posted - the status code will be present IF (and only IF) the host responds in time (e.g., busy host or slow network, etc)

If you want to guard against a non-response you'll have to wrap the REST call in a try/catch block and put that inside a loop

e.g.

function waitABit(timeToWaitInSeconds)

{

try

{

var now = System.getCurrentTime();

var d = new Date();

var waitMS = now + (timeToWaitInSeconds * 1000); // convert to MS

d.setTime(waitMS);

System.waitUntil(d);

return true;

}

catch(error)

{

System.log("Got error on wait!");

return false;

}

}

var restResponseContent = null;

var MAX_ATTEMPTS = 5;

var delay = 10;

for(var i=0; i<MAX_ATTEMPTS; i++)

{

try

{

// execute REST call here & handle responses if you get one

var response = execMyRESTCallHere();

if(response.statusCode > 400)

{

System.log("Server returned error - wait and try again");

}

else

{

// handle good response

restResponseContent = response.contentAsString;

break;

}

}

catch(error)

{

System.log("REST call failed to respond in time!");

}

// wait here

waitABit(delay);

}

// must have this here in case you ran out of attempts and fell through!

if(restResponseContent != null)

{

// use the content

}

Anyway, HTH

View solution in original post

Reply
0 Kudos
4 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

The response object contains also the status code as a number. In your sample, you should be able to retrieve it with something like (after line 12):

var status = response.statusCode;

if (status >= 400) {

    System.log("Got an error status code");

} else {

    // ...

}

Reply
0 Kudos
eoinbyrne
Expert
Expert
Jump to solution

Just to build on what iiliev​ has posted - the status code will be present IF (and only IF) the host responds in time (e.g., busy host or slow network, etc)

If you want to guard against a non-response you'll have to wrap the REST call in a try/catch block and put that inside a loop

e.g.

function waitABit(timeToWaitInSeconds)

{

try

{

var now = System.getCurrentTime();

var d = new Date();

var waitMS = now + (timeToWaitInSeconds * 1000); // convert to MS

d.setTime(waitMS);

System.waitUntil(d);

return true;

}

catch(error)

{

System.log("Got error on wait!");

return false;

}

}

var restResponseContent = null;

var MAX_ATTEMPTS = 5;

var delay = 10;

for(var i=0; i<MAX_ATTEMPTS; i++)

{

try

{

// execute REST call here & handle responses if you get one

var response = execMyRESTCallHere();

if(response.statusCode > 400)

{

System.log("Server returned error - wait and try again");

}

else

{

// handle good response

restResponseContent = response.contentAsString;

break;

}

}

catch(error)

{

System.log("REST call failed to respond in time!");

}

// wait here

waitABit(delay);

}

// must have this here in case you ran out of attempts and fell through!

if(restResponseContent != null)

{

// use the content

}

Anyway, HTH

Reply
0 Kudos
eoinbyrne
Expert
Expert
Jump to solution

Sorry about crap indentation there - not sure what happened & editing seems to make it worse!

I've attached the snippet as TXT below FWIW

Reply
0 Kudos
craigso
Enthusiast
Enthusiast
Jump to solution

Thank you for the replies everyone. This looks like exactly what I needed!

Reply
0 Kudos