Mr Robot Mr Robot - 8 months ago 88
Node.js Question

Node - Custom error helpers

Trying to create custom helper methods to avoid repetitions, and i'm using

express-promise-router


app.js created the error handler middle ware

//errorHandler
app.use((err, req, res, next) => {
//const error = app.get('env') === 'development' ? err : {};
const error = err;
const code = err.code || '';
const status = err.status || 500;

res.status(status).json({
isresponse: 1,
res_time: Date(),
res_id: 'tempororyid',
res_data: {
error: {
code: code,
message: error.message
}
}
});

console.log(err);
});


errorHelper.js for all the error helper methods

module.exports = {
notFound: (req, res, next) => {
const err = new Error('url not found please check the documentation');
err.status = 404;
err.code = 'URLNotFound';
next(err);
},

emailExist: (req, res, next) => {
const err = new Error('the email is already associated with another user account');
err.code = 'EmailUsed';
err.status = 400;
next(err);
}
};


authController.js for authentication

const User = require('../models/User');
const { emailExist } = require('../helpers/errorHelper');

module.exports = {
signup: async (req, res, next) => {

const userGuid = guid();
const email = req.body.email;

const existingUser = await User.findOne({email: email});

if(existingUser) {
emailExist();
}

const newUser = await User.create(req.body);

res.status(200).json({
isresponse: 1,
res_time: Date(),
res_id: 'TEMPID002',
res_data: {
success: 1,
data: []
}
});
}
}


Getting the following error

TypeError: next is not a function
at emailExist (D:\Workspace\path\path\path\errorHelper.js:13:4)
at signup (D:\Workspace\path\path\path\authController.js:26:4)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
POST /api/v1/signup 500 201.262 ms - 176


But if throwing error as below things works as expected

if(existingUser) {
const error = new Error('the email is already associated with another user account');
error.code = 'CR_NU_EmailUsed';
error.status = 400;
next(error);
}


looking for much needed help

thank you

Answer Source

module.exports = {
    notFound: (next) => {
        const err = new Error('url not found please check the documentation');
            err.status = 404;
            err.code = 'URLNotFound';
            return err;
    },

    emailExist: (next) => {
        const err = new Error('the email is already associated with another user account');
            err.code = 'EmailUsed';
            err.status = 400;
            return err;
    }
};

const User = require('../models/User');
const { emailExist } = require('../helpers/errorHelper');

module.exports = {
    signup: async (req, res, next) => {

        const userGuid = guid();
        const email = req.body.email;

        const existingUser = await User.findOne({email: email});

        if(existingUser) {
           return next(emailExist()); 
        }

        const newUser = await User.create(req.body);

        res.status(200).json({
            isresponse: 1,
            res_time: Date(),
            res_id: 'TEMPID002',
            res_data: {
                success: 1,
                data: []
            }
        });
    }
}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download