what's the best way to handle errors in a restlet?...
# suitescript
m
what's the best way to handle errors in a restlet? testing using postman to post data into NS to create case records. should this fail for any reason could we send back a failure message back to the calling application?
n
you can only return data and not error i.e if you throw error or there is some uncaught error, it would result in system error in script-logs, so its better to return a object like,
Copy code
{ success: true, data: SUCCES_DATA }
or
Copy code
{ success: false, error: ERROR_REASON }
Note: 1. Use try-catch block 2. check request type before returning. i.e check typeof requets object, if it is JSON, your return format should be the same
m
thanks. would you suggest something like?
Untitled
n
you need to make sure type fo return object is the same as request object. i.e if request-object(scriptContext) is object, return object type and if it is string, you should return string
m
my request object would be the restletBody in this case?
Untitled
n
This is what I meant,
Copy code
return typeof restletBody === 'string' 
  ? JSON.stringify(successObj)
  : successObj;
where restletBody is your request object
since if your return object is not of the same type, NetSuite returns invalid return-type error
m
thanks for that. i entered bad data to see what the catch looks like but it never enters - appears to throw error within the try