I have a simple POST function consuming axios.post
to send emails. It works fine, but I can't catch errors.
I have tried both ways with no success:
export async function postEmail(data) {
const { subject, message, emailAddress } = data
const body = {
email: emailAddress,
subject: subject,
message: message
}
const url = process.env.EMAIL_API_URL
const response = await axios.post(url, body)
if (response.status === 200) { return response }
else { throw 'Something wrong' }
}
And tried it with a promise:
export function postEmail(data) {
const { subject, message, emailAddress } = data
const body = {
email: emailAddress,
subject: subject,
message: message
}
const url = process.env.EMAIL_API_URL
return new Promise((resolve, reject) => {
try {
const response = axios.post(url, body)
resolve(response)
} catch (error) {
console.log(error)
reject(error)
}
})
}