我有一个简单的loopback.js应用程序,其中包含strong-error-handler https://github.com/strongloop/strong-error-handler和以下middlewares/middleware.json代码
"final:after": {
"./middlewares/log-error": {},
"strong-error-handler": {
"params": {
"debug": false,
"log": true
}
}
}我的middlewares/log-error文件如下所示
module.exports = function createErrorLogger(options) {
return function logError(err, req, res, next) {
// your custom error-logging logic goes here
console.log("inside custom error logging");
const status = err.status || err.statusCode;
if (status >= 500) {
// log only Internal Server errors
console.log('Unhandled error for request %s %s: %s',
req.method, req.url, err.stack || err);
}
// Let the next error handler middleware
// produce the HTTP response
next(err);
};
}我有一个简单的函数,如下所示
function myfunc(){
myvar.asdf #myvar is undefined
}在上述函数中,myvar是未定义的。因此,当我调用这个函数http://localhost:3001/myfunc时,它抛出了这个错误,应用程序崩溃
我想避免应用程序崩溃。一个简单的try/catch有助于避免这个错误,但是有没有一种方法可以避免在发生这样的错误时节点崩溃呢?
发布于 2019-09-11 13:14:37
使用uncaughtException,因为您的应用程序不会崩溃,并且您可以选择如何处理错误。
process.on('uncaughtException', function (err) {
console.log(err);
})https://stackoverflow.com/questions/57882193
复制相似问题