How to handle rest api timeout in script?

Snehal13
Kilo Sage

Need to inform user (who is triggering the api call)

 

If api timeout is 60 sec and it's overrun this time, it must give a message to user.

 

How to achieve this, I am using rest message v2 api in a server script 

5 REPLIES 5

M Ismail
Tera Guru

Hi @Snehal13 ,

// Or 'POST', 'PUT', etc.
restMessage.setRequestBody('YOUR_REQUEST_BODY'); // If needed
restMessage.setHttpTimeout(60000); // Set timeout to 60 seconds (in milliseconds)

// Execute the REST Message
var response = restMessage.execute();

// Check for errors
if (response.getStatusCode() != 200) {
// Check if the error is due to timeout
if (response.getStatusCode() == 504) {
// Send message to user
gs.addErrorMessage('The API call timed out. Please try again later.');
} else {
// Handle other errors
gs.error('An error occurred: ' + response.getStatusCode() + ' - ' + response.getErrorMessage());
}
} else {
// Process response if available
var responseBody = response.getBody();
// Your response processing logic here
}


Please Like and mark my reply as the answer if it solve your probelm.
Thank you 
M.Ismail

How to handle api timeout and show message to user from a flow ? 

Flow runs in background and doesn't have interaction with the UI/form.

So not possible.

But If you want, you can add logging statements, that will show up in the system logs.

 

piyushsain
Tera Guru
Tera Guru

Hi @Snehal13 

API has already predefined error for timout so whenever the time exceeds 60 seconds the API will give an error for timeout. If you want to customize your error you can edit in your script 

(function run(request, response) {      
  var myError = new sn_ws_err.ServiceError();
  myError.setStatus(500);
  myError.setMessage('Timeout error more than 60 seconds wait');
  myError.setDetail('enter the detials');
  return myError;
})(request,response);
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain