Has anyone had an issue using the async keyword in entry point functions such as post or get in RESTlet script types?
Our team has noticed that if you make a entry point function like post async, it results in an unexpected error if you throw anything. For example, the simple script below, it is responding with an unexpected error:
/**
*@NApiVersion 2.1
*@NScriptType Restlet
*/
define(
["N/error"],
function (error) {
async function _post(context) {
throw error.create({ name: "MY_ERROR", message: "This is my error" })
}
return {
post: _post,
};
});
JSON Response:
{
"error": {
"code": "UNEXPECTED_ERROR",
"message": "An unexpected SuiteScript error has occurred"
}
}
We would have expect it to respond with this though:
{
"error": {
"code": "MY_ERROR",
"message": "{\"type\":\"error.SuiteScriptError\",\"name\":\"MY_ERROR\",\"message\":\"This is my error\",\"stack\":[\"Error\",\" at Object._post (/SuiteScripts/entry_point/restlet/emerson_ecologics/sales_order/sales_order.js:11:25)\"],\"cause\":{\"name\":\"MY_ERROR\",\"message\":\"This is my error\"},\"id\":\"\",\"notifyOff\":false,\"userFacing\":true}"
}
}
In fact, if we remove async it throws the error just fine and we get the response right above. Does anyone know if we're missing something or why it might be doing this?
There doesn't seem to be an issue with return some value, but it doesn't like the throw here. We also confirmed that there isn't really an issue throwing an error in an async function within a module, however it's really when we throw the error at the 'entry point' level (e.g. RESTlet script type) there's an issue.
We need this function to be async in our real scenario since it's going to be running some asynchronous code. Which may throw some error, which needs to be shown in the request response.