Reputation: 1869
Tried these solution
Nothing worked. So I', trying to throw my own exception from within sendMail errors like this
try{ transporter.sendMail(mailOptions, (error, response) => {
if(error){
console.log(`couldn't send mail ${error}`);
throw 500;
}else{
console.log('Message sent: %s', response);
}
});
} catch (error) {
console.log('error =' + error);
}
I'm getting an error:
throw 500;
^
500
(Use `node --trace-uncaught ...` to show where the exception was thrown)
app crashed - waiting for file changes before starting...
How can I return a response success or error to my client?
Upvotes: 1
Views: 1759
Reputation: 814
In javascript, try/catch block does not catch an exception that is throwed from a callback in a function which returns a promise.
transporter.sendMail
function returns a promise. So, try/catch block can not catch the exception from the callback function.
You can do this, instead;
try {
const response = await transporter.sendMail(mailOptions);
console.log('Message sent: %s', response);
} catch (error) {
console.log('error =' + error);
throw 500; // or throw Error("your error message");
}
Upvotes: 2