{"size": 960, "ext": "js", "max_stars_repo_path": "backend/middlewares/errors.js", "max_stars_repo_name": "kvlin/ShopNow", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "backend/middlewares/errors.js", "max_issues_repo_name": "kvlin/ShopNow", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "backend/middlewares/errors.js", "max_forks_repo_name": "kvlin/ShopNow", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.9677419355, "max_line_length": 84, "alphanum_fraction": 0.6052083333}
const ErrorHandler = require ('../utils/errorHandler');

//express knows this middleware is for error handling because it has
 //* four parameters (err, req, res, next)
module.exports = (err, req, res, next) => {
    err.statusCode = err.statusCode || 500; // 500 by default meaning internal error
    err.message = err.message || 'Internal server error';

    // Error handling if in DEVELOPMENT mode
    if(process.env.NODE_ENV == 'DEVELOPMENT'){
        res.status(err.statusCode).json({
            success:false,
            error: err,
            errMessage: err.message, // more detailed message
            stack: err.stack
        })
    }

    // Error handling if in PRODUCTION mode
    if(process.env.NODE_ENV == 'PRODUCTION'){
        let error = {...err}

        error.message = err.message 

        res.status(error.statusCode).json({
            success: false,
            message: err.message || 'Internal Server Error'
        })
    }

}