как обрабатывать ошибки на FrontEnd MERN - PullRequest
0 голосов
/ 09 ноября 2018

У меня есть приложение MERN с намерением собирать данные в форме, и оно отправляет их на сервер, а nodemailer отправляет почту получателю. Все отлично работает, за исключением ошибок на front-end. Я настроил validator в моем приложении Express, и оно улавливает все ошибки, как и ожидалось, но только в почтальоне. Когда дело доходит до React, я получаю только

POST http://localhost:5000/send 400 (Bad Request)

Error: Request failed with status code 400
at createError (createError.js:17)
at settle (settle.js:19)
at XMLHttpRequest.handleLoad (xhr.js:78)

У меня есть errors объект, который заполняется и отправляется в случае возникновения ошибки ...

app.post('/send', (req, res) => {
const {
    errors,
    isValid
} = validatedInput(req.body);

// Check Validation
if (!isValid) {
    return res.status(400).json(errors);
}
const output = `
<h1>New Adverse Event reported!</h1>
<ul style="list-style:none;">
    <li>Name: <h3>${req.body.name}</h3></li>
    <li>Lastname: <h3>${req.body.lastname}</h3></li>
    <li>Location: <h3>${req.body.location}</h3></li>
    <li>Phone: <h3>${req.body.phone}</h3></li>
    <li>Phone: <h3>${req.body.email}</h3></li>
    <li>Date: <h3>${req.body.date}</h3></li>
    <li>Drug: <h3>${req.body.drug}</h3> </li>
    <li>Batch number: <h3>${req.body.batch}</h3></li>
    <li>Adverse Event Outcome: <h3>${req.body.outcome}</h3></li>
</ul>
<p>
Adverse Event details: <br>
    ${req.body.details}
</p>
`
let transporter = nodemailer.createTransport({
    host: process.env.SMTP,
    port: process.SMTP_PORT,
    secure: true, // true for 465, false for other ports
    auth: {
        user: process.env.AUTH_USER, // generated ethereal user
        pass: process.env.AUTH_PASS // generated ethereal password
    },
    tls: {
        rejectUnauthorized: false
    }
});

// setup email data with unicode symbols
let mailOptions = {
    from: '"Adverse Event Pharma" <foo@example.com>', // sender address
    to: '',
    subject: `New AE has been reported for ${req.body.drug}`, // Subject line
    html: output // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error);
    }
    console.log('Message sent: %s', info.messageId);
    // Preview only available when sending through an Ethereal account
    console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
    res.status(200).json({
        'msg': 'Message has been sent'
    })
});

})

У меня вопрос, как извлечь эти ошибки на внешний интерфейс и отобразить их?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...