Should throwing an error in a RESTlet output the e...
# suitescript
s
Should throwing an error in a RESTlet output the error message in the response? I want to throw custom errors and it's not returning any error. I'm also using async functions, so I'm wondering if it is related to that too.
n
It should if you are returning the error out of the restlet. But if you aren’t explicitly returning it then it won’t.
Copy code
try{
//do stuff
return stuff
} catch (e) {
log.error({title: “error doing stuff”,details: e});
return JSON.stringify(e)
}
s
I was hoping that the error could be returned to whatever is calling the service as an error (i.e. 400,500) instead of a success (200).
So sticking it a try/catch might work. I think I get it now.
s
No, the returned HTTP codes are not controllable by your code. RESTlets are not RESTful.
n
@Shawn Talbert so is it basically just "you got to the function block so its successful" And ignores what may happen inside the post,put,delete, and get?
e
I believe if your Restlet `return`s normally, the client will receive a `200`; if your Restlet `throw`s, the client will receive a
400
You cannot control the response code any further than that. For teams that need that, I usually see them return an object that includes an error code and message, then leave it to the client to inspect
🫡 1
j
I usually do something like this:
Copy code
throw error.create({
				name: 'FAILURE',
				message: result.error,
				notifyOff: false
			});
☝️ 1
👍 1
where result.error has whatever error I wanna return
s
because of this limitation, we typically do what Eric mentioned - elevate errors to be first class citizens and a documented optional part of every response from every restlet.
Doing so also supports representing the real-world situation of partial success/failure if applicable.
so basically all our restlets return a shape roughly like
{ data: 'some successful response data', errors?: [ 'zero', 'or', 'more', 'errors'] }
145 Views