簡體   English   中英

我的錯誤處理代碼的哪一部分可能導致 Heroku 崩潰?

[英]What part of my error handling code could be causing Heroku to crash?

問題:我在我的代碼中引入了錯誤處理,它應該在出現問題時呈現錯誤頁面和錯誤消息。 在此特定實例中,當用戶嘗試在未登錄的情況下訪問受保護路由時,應該會顯示一個錯誤頁面。相反,Heroku 只會等待 30 秒並崩潰。 奇怪的是,在 LocalHost 上一切都按預期工作。

雖然我不確定,但我相信錯誤來自下面代碼中的某處(當調用 const sendErrorDev function 時)。 在我介紹這個之前,一切正常。 但是,我不確定是什么導致 Heroku 與 LocalHost 上的結果不同。

const AppError = require('../utils/appError');

const JsonWebTokenError = () =>
  new AppError('Invalid token. Please login again!', 401);

const handleJWTExpiredError = () =>
  new AppError('Your token has EXPIRED! What?!', 401);

const handleCastErrorDB = err => {
  const message = `Invalid ${err.path}: ${err.value}.`;
  return new AppError(message, 400);
};

const handleDuplicateFieldsDB = err => {
  const value = err.errmsg.match(/(["'])(?:(?=(\\?))\2.)*?\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, req, res) => {
  // A. API
  if (req.originalUrl.startsWith('/api')) {
    return res.status(err.statusCode).json({
      status: err.status,
      error: err,
      message: err.message,
      stack: err.stack
    });
  }
  // B. RENDERED WEBSITE
  return res.status(err.statusCode).render('error', {
    title: 'Something went wrong!',
    msg: err.message
  });
};

const sendErrorProd = (err, req, res) => {
  // A) API
  console.log('This is working!!!');
  if (req.originalUrl.startsWith('/api')) {
    // A) Trusted operaitonal error, okay to send the details to the client.
    if (err.isOperational) {
      return res.status(err.statusCode).json({
        status: err.status,
        message: err.message
      });
    }
    // Weird coding errors, don't leak the details to the client.
    // 1.) Log the error to the
    console.error('ERROR!', err);
    // 2.) Send generic message to the client
    return res.status(500).json({
      status: 'error',
      message: 'Something went very wrong! JESUS!'
    });
  }
  // B) Rendered Website
  if (err.isOperational) {
    console.log(err);
    return res.status(err.statusCode).render('error', {
      title: 'Something went wrong!',
      msg: err.message
    });
  }
  // Weird coding errors, don't leak the details to the client.
  // 1.) Log the error to the
  console.error('ERROR!', err);
  // 2.) Send generic message to the client
  return res.status(err.statusCode).render('error', {
    title: 'Something went wrong!',
    msg: 'Please try again later.'
  });
};

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, req, res);
  } else if (process.env.NODE_ENV === 'production') {
    let error = { ...err };
    error.message = err.message;
    if (error.name === 'CastError') error = handleCastErrorDB(error);
    if (error.code === 11000) error = handleDuplicateFieldsDB(error);
    if (error.name === 'ValidationError')
      error = handleValidationErrorDB(error);
    if (error.name === 'JsonWebTokenError') error = JsonWebTokenError();
    if (error.name === 'TokenExpiredError') error = handleJWTExpiredError();

    sendErrorProd(error, req, res);
  }
};

在 Heroku 中,我拼錯了一個配置變量。 在我更正之后,問題就消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM