Hi All, I trying to do a simple throw error in a R...
# suitescript
r
Hi All, I trying to do a simple throw error in a Restlet
Copy code
throw  error.create({name: 'NO_ORDERS_FOUND', message: 'No Matching Orders', notifyOff: true});
But I seem to get the stack trace as well. Any idea how to only show the
name
and
message
. POSTMan returns HTTP : 400 (which is what i want) but ugly error
Copy code
{
  "error": {
    "code": "NO_ORDERS_FOUND",
    "message": "{\"type\":\"error.SuiteScriptError\",\"name\":\"NO_ORDERS_FOUND\",\"message\":\"No Matching Orders\",\"stack\":[\"Error\",\"    at processPayload (/SuiteScripts/POS/rest_infinity_receipt_no.js:89:22)\",\"    at <http://Object.post|Object.post> (/SuiteScripts/POS/_rest_infinity_receipt_no.js:53:7)\"],\"cause\":{\"name\":\"NO_ORDERS_FOUND\",\"message\":\"No Matching Orders\",\"notifyOff\":true},\"id\":\"\",\"notifyOff\":true,\"userFacing\":true}"
  }
}
I just want a cleaner error message. I don't see any other options.
b
take a look at the code for your errors's .toJSON method so you understand how that message is generated
after that, overwrite the errors's .toJSON method to do what you want
keep in mind that you will have to keep the type key and its value, its used to determine what is shown in the response
r
my other thought was to have a TRY catch inside the restlet and have a custom return message but that gives HTTP 200 as the error is caught... so wanted to know what others do
b
you have to throw something to get a 400
you could throw a normal Error, or even a string if you want
r
yeah correct..
thats what I thought I was doing
throw  error.create({name: 'NO_ORDERS_FOUND', message: 'No Matching Orders', notifyOff: true});
b
normal javascript Error
prints somewhat differently, but is a lot less weird than overwriting .toJSON
e
Copy code
throw {
  name: "The name",
  message: "The message"
};