【问题标题】:destructuring of the orignal error to return invalid data of mongodb errors with an array解构原始错误以使用数组返回 mongodb 错误的无效数据
【发布时间】:2020-09-12 16:55:07
【问题描述】:

我正在使用 Express 的错误处理,并且我曾经使用数组返回 MongoDB 错误的无效数据

我用过

else if (process.env.NODE_ENV === 'production') {
    
    let error = { ...err };

    if (error.name === 'CastError') error = handleCastErrorDB(error);
    if (error.code === 11000) error = handleDuplicateFieldsDB(error);
    if (error.name === 'ValidationError')
      error = handleValidationErrorDB(error);

    sendErrorProd(err, res);
  }

但它不适用于 process.env.NODE_ENV === 'production'

因为 let (error = { ...err };)

【问题讨论】:

  • 你想用let error = { ...err };做什么? (一方面,这不是解构。它是休息符号。)
  • 如果问题是它在生产环境中不起作用,那么您的生产环境似乎不支持 ES2018 中添加的对象休息表示法。如果您将环境中的 Node 版本升级到可以接受的最新版本,它应该会开始工作。
  • “它不起作用”是什么意思?
  • 我要显示所有的(CastError,ValidationError,11000) 不是一个一个
  • 它适用于我的这个

标签: javascript mongodb error-handling


【解决方案1】:

err 是否有可枚举的属性?我的猜测是it doesn't,因此对象传播语法不会将任何内容复制到对象文字中。您可能只想使用原始的err 对象,我认为您的代码应该是

if (process.env.NODE_ENV === 'production') {
    let error;
    if (err.name === 'CastError')
        error = handleCastErrorDB(err);
    else if (err.code === 11000)
        error = handleDuplicateFieldsDB(err);
    else if (err.name === 'ValidationError')
        error = handleValidationErrorDB(err);
    else
        error = { ...err };

    sendErrorProd(err, res);
}

【讨论】:

    【解决方案2】:

    它和我的一样工作

    else if (process.env.NODE_ENV === 'production') {
        //
    
        if (err.name === 'CastError') err = handleCastErrorDB(err);
        if (err.code === 11000) err = handleDuplicateFieldsDB(err);
        if (err.name === 'ValidationError')
          err = handleValidationErrorDB(err);
    
        sendErrorProd(err, res);
      }

    但输出看起来像这样

    {
        "status": "fail",
        "message": "Duplicate field value: \"The Snow Adventurer2\". Please use another value!"
    }

    这只是 11000 错误,我还有一个 CastError 和 ValidationError 错误。

    我需要在一条消息中显示所有错误。

    这是所有代码

    const handleCastErrorDB = err => {
      const message = `Invalid ${err.path}: ${err.value}.`;
      return new AppError(message, 400);
    };
    
    const handleDuplicateFieldsDB = err => {
      const value = err.errmsg.match(/(["'])(\\?.)*?\1/)[0];
      console.log(value);
    
      const message = `Duplicate field value: ${value}. Please use another value!`;
      return new AppError(message, 400);
    };
    const handleValidationErrorDB = err => {
      const errors = Object.values(err.errors).map(el => el.message);
    
      const message = `Invalid input data. ${errors.join('. ')}`;
      return new AppError(message, 400);
    };
    
    const sendErrorDev = (err, res) => {
      res.status(err.statusCode).json({
        status: err.status,
        error: err,
        message: err.message,
        stack: err.stack
      });
    };
    
    const sendErrorProd = (err, res) => {
      // Operational, trusted error: send message to client
      if (err.isOperational) {
        res.status(err.statusCode).json({
          status: err.status,
          message: err.message
        });
    
        // Programming or other unknown error: don't leak error details
      } else {
        // 1) Log error
        console.error('ERROR ?', err);
    
        // 2) Send generic message
        res.status(500).json({
          status: 'error',
          message: 'Something went very wrong!'
        });
      }
    };
    
    module.exports = (err, req, res, next) => {
      // console.log(err.stack);
    
      err.statusCode = err.statusCode || 500;
      err.status = err.status || 'error';
    
      if (process.env.NODE_ENV === 'development') {
        sendErrorDev(err, res);
      } else if (process.env.NODE_ENV === 'production') {
        
        let error = { ...err };
    
        if (error.name === 'CastError') error = handleCastErrorDB(error);
        if (error.code === 11000) error = handleDuplicateFieldsDB(error);
        if (error.name === 'ValidationError')
          error = handleValidationErrorDB(error);
    
        sendErrorProd(err, res);
      }
    };

    【讨论】:

    • edit your question 而不是发布答案(除非您自己找到了解决方案)
    • 对不起@Bergi,但这是我在 StackOverflow 中的第一个问题,我会自己找到解决方案
    猜你喜欢
    • 2020-09-12
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多