user13708746
user13708746

Reputation:

How do I prevent my node.js error middleware function from failing on subsequent function calls?

I'm writing an auth. route for a node.js webserver. I'm using Express and it works well.

What I'm having trouble with is invoking a sendMail() function when an error occurs. At the moment, I'm just throwing a new error in the route to test.

throw new Error('testing...');

Because I'm using the express-async-errors module, if an error occurs in a route, program flow goes to my error middleware function.

It works perfect.

But in this error middleware function, I call my sendMail() function, which immediately fails giving me the same error as above: 'testing...'

const sendEmail = require('../email/sendEmail');


// error middleware function
async function error(err, req, res, next) {
        const wasSent = await sendEmail('[email protected]', 'Error', err);
        console.log(wasSent);

        // http 500 - internal server error
        res.status(500).send("Something unexpected happened. Our team has been notified.");
}

The result is my catch block in the below sendMail() function gets called. What am I doing wrong?

Here's my sendmail function:

const nodemailer = require("nodemailer");
const striptags = require('striptags');
const config = require('config');


async function sendEmail(to, subject, content) {
        try {
                // create reusable transporter object using the default smtp transport
                let transporter = nodemailer.createTransport({
                        host: config.get('emailHost'),
                        port: config.get('emailPort'),
                        secure: config.get('emailSecure'), // true for 465, false for other ports
                        auth: {
                                user: config.get('emailUsername'),
                                pass: config.get('emailPassword'),
                        },
                });

                // send mail with defined transport object
                let info = await transporter.sendMail({
                        from: config.get('emailFrom'), // sender address
                        to: to, // list of receivers
                        subject: subject,
                        text: striptags(content), // plain text body
                        html: content, // html body
                });

                // e.g. message sent: <[email protected]>
                //console.log("message sent: %s", info.messageId);

                return true;
        }
        catch(ex) {
                console.log("error: sendEmail() invocation failed;", ex);
                return false;
        }
}

module.exports = sendEmail;

Upvotes: 0

Views: 45

Answers (1)

user13708746
user13708746

Reputation:

The error was the "err" object. Once I converted this to a string using UTIL it worked.

const util = require('util');

...
const fullErrorTrace = util.inspect(err, { showHidden: false, depth: null }).replace(/[\r\n]/gm, '<br />');

const wasSent = await sendEmail('[email protected]', 'Error', fullErrorTrace);
...

Upvotes: 0

Related Questions