How to handle rest api timeout in script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 01:13 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 01:30 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 05:10 AM
How to handle api timeout and show message to user from a flow ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 10:36 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 01:47 AM
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);
Regards,
Piyush Sain