gpt4 book ai didi

javascript - 我的错误处理代码的哪一部分可能导致Heroku崩溃?

转载 作者:行者123 更新时间:2023-12-03 08:39:15 26 4
gpt4 key购买 nike

问题:我在代码中引入了错误处理,如果出现问题,该错误处理应呈现一个错误页面和一条错误消息。在此特定情况下,当用户尝试未登录而访问保护路由时,将显示错误页面。相反,Heroku仅等待30秒并崩溃。奇怪的是,一切都在LocalHost上按预期工作。
虽然我不确定,但我认为错误是来自以下代码中的某处(调用const sendErrorDev函数时)。一切工作都找到了,直到我介绍了这一点。但是,我不确定是什么导致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中,我拼错了一个配置变量。我更正后,问题消失了。

关于javascript - 我的错误处理代码的哪一部分可能导致Heroku崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63494649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com