How to properly handle throw in beforeSave

We have a cloud function which creates a Client object but sometimes it can fail so we have
try
{
/// here we try to create some Client object and it fails in beforeSave (we throw ‘some error

}catch(error){
// here error isn’t just ‘some error’ but also
'Received an error with invalid JSON from Parse: Error: some error.< br >    at error (/opt/server/node_modules/parse-server/lib/triggers.js:379:16)
   at process._tickCallback (internal/process/next_tick.js:68:7)
}

How to avoid that strange mix of html and our throw message, stripping it out manually sounds like a bit bad pattern. We want to get that throw message and pass it as a response to the client.

I’d log it for your reference and just throw new Error(‘A better message’) to the user.

But even if I throw new Error it’s still wrapped in : Received an error with invalid JSON from Parse: Error: some new error .< br > at error (/opt/server/node_modules/parse-server/lib/triggers.js:379:16)
at process._tickCallback (internal/process/next_tick.js:68:7)

Where do you see this wrapped error?

I’m getting such errors in the client, maybe I should reconfigure something?
Another sample is for problems with login - on a web client (javascript Vue.js) I get such response, how can I even parse such a response? Maybe it’s something to do with express configuration?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Error: Invalid session token<br> &nbsp; &nbsp;at Object.getAuthForSessionToken (/opt/server/node_modules/parse-server/lib/Auth.js:114:11)<br> &nbsp; &nbsp;at process._tickCallback (internal/process/next_tick.js:68:7)</pre>
</body>
</html>

It may be the express setup or another proxy you may have In the server side. Could you share the code that you are using to create your express app?

Ok I’ve handled it, thank you for your help. It was just enough to add to index.js:

app.use(function(err, req, res, next) {
  res.send({result:err});
})

I’m happy we can continue to work finally. Not sure about the stack trace, but at least we are not getting strange HTML with the response.