0

I'm trying to set the error name err.name = 'ExpressValidatorError';

of an custom Error class class AppError extends Error

that is passed to centralErrorHandler to filter and handle errors by err.name.

I have did a lot of research but still couldn't figure out why err.name in centralErrorHandler console logs as undefined.

When I change return next(err); in auth.controller.js to throw err;, the err.name does console log as 'ExpressValidatorError' but i'm not sure if using throw is correct.

centralErrorHandler.js

module.exports = (err, req, res, next) => {    
 err.statusCode = err.statusCode || 500;
 err.status = err.status || 'error';   
      
 let error = {...err};

 console.log(error.name);

 if(err.name === 'ExpressValidatorError') error = handleExpressValidatorError(err);    

 if(err.name === 'InternalOAuthError') error = handleInternalOAuthError(err);    

 res.status(error.statusCode).json({
   status: error.status,            
   message: error.message            
 });  
}

auth.controller.js

const {validationResult} = require('express-validator');

exports.signup = (req, res) => {     
    const errors = validationResult(req); 
   
      if (!errors.isEmpty()) {
        let err = new AppError(`Invalid login credentials.`, 422);
        err.name = 'ExpressValidatorError';            
             
        return next(err);
      }

    res.status(200).send(req.user);          
}

appError.js

class AppError extends Error {
    constructor(message, statusCode){
        super(message);

        this.statusCode = statusCode;
        this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';        
        this.isOperational = true;        

        Error.captureStackTrace(this, this.constructor);
    }
}

module.exports = AppError;
2
  • It seem working fine on my local, what is your nodejs version? Commented Jul 11, 2020 at 4:40
  • Hi, here are the version details: "engines": { "node": "12.18.1", "npm": "6.14.5" } Commented Jul 11, 2020 at 4:41

1 Answer 1

1

Why not set the property name like this:

class AppError extends Error {
    constructor(message, statusCode, name){
        super(message);
        this.name = name;
        this.statusCode = statusCode;
        this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';        
        this.isOperational = true;        

        Error.captureStackTrace(this, this.constructor);
    }
}

module.exports = AppError;

This way the property name will be bound to the object of AppError.

And middleware like this:

const {validationResult} = require('express-validator');

exports.signup = (req, res, next) => {     
    const errors = validationResult(req); 
   
      if (!errors.isEmpty()) {
        let err = new AppError(`Invalid login credentials.`, 422);
        err.name = 'ExpressValidatorError';            
             
        return next(err);
      }

    res.status(200).send(req.user);          
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi, i tried that let err = new AppError('Invalid login credentials.', 422, 'ExpressValidatorError'); but don't know why the error.name passed to the centralErrorHandler still console log as undefined.
That shouldn't be can you please try consoling err and then try consoling the error object itself I'm a little sceptic that data might be getting lost while {...err}
when i console.log(err); in centralErrorHandler, it returns ReferenceError: next is not defined. You are right, the err object is lost when it reach centralErrorHandler.js
Updated the answer to explicitly include next can you please try that now?
exports.signup = (req, res, next) => { this line
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.