如何防止节点在我的 NextJS/Express 应用程序中将异常记录到控制台?
How do I prevent node from logging an exception to the console in my NextJS/Express app?
我有一个在 NextJS 应用程序中运行博客的 Express 应用程序,与 example in their repo
非常相似
我已将其设置为让我的应用运行查询以获取博客文章,如果结果为空,则会抛出 NotFoundException
.
我在我的 NextJS _error.js
文件中捕获了这个异常,它类似于 React 错误边界,我在其中将用户路由到我的 404 页面。这部分工作正常。
我遇到的问题是这个异常被记录到节点控制台,即使我在捕获异常时没有记录它。这会用我们所有的 404
污染我们公司的日志记录软件
这里是否缺少一些 node/express 设置来防止记录异常?这是我的 Express 进程错误处理程序:
process.on('unhandledRejection', (reason, promise) =>
console.error(`Unhandled Rejection at: ${promise}.\nreason: ${reason.stack || reason}`));
我知道那里有一个日志,但我要删除的格式与此不同,所以我确信这不是来源。
在 express 中,您可以设置一个 ErrorMiddleware。
在你所有的路线声明之后,把
server.use(function(req, res, next) {
handler(req, res).catch(e => {
// use rejected promise to forward error to next express middleware
next(e)
})
});
像这样,当你拒绝一个 Promise 时,next(e) 会将你的错误发送给下一个中间件。我通常在发送错误的地方设置一个中间件,然后我在一个函数中管理所有错误(基于 statusCode 错误,...)。
我不会假装知道发生了什么,但我最好的猜测是 next.js 正在某处记录错误。我做了一些挖掘,看起来服务器代码中有一个错误记录器,它将记录错误,除非在服务器上设置 quiet
属性:
https://github.com/zeit/next.js/blob/canary/packages/next-server/server/next-server.ts#L105:
return this.run(req, res, parsedUrl)
.catch((err) => {
this.logError(err)
res.statusCode = 500
res.end('Internal Server Error')
})
这是 logError 函数的签名和正文:
private logError(...args: any): void {
if (this.quiet) return
// tslint:disable-next-line
console.error(...args)
}
如果您查看有关将下一个 API 与自定义服务器一起使用的文档,它会指出可以传递给构造函数的以下选项对象属性:
The next API is as follows:
next(opts: object)
Supported options:
dev (bool) whether to launch Next.js in dev mode - default false
dir (string) where the Next project is located - default '.'
quiet (bool) Hide error messages containing server information - default false
conf (object) the same object you would use in next.config.js - default {}
在构造下一个对象时,尝试将 quiet 传递为 true 以查看是否可以解决您的问题:
const express = require('express')
const next = require('next')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev, quiet: true })
const handle = app.getRequestHandler()
文档还提到在非生产环境中记录错误(在 process.env.NODE_ENV !== 'production'
时识别),因此我也会检查以确保您将 NODE_ENV 设置为 'production'开始申请:
NODE_ENV=production node server.js
希望对您有所帮助!
我有一个在 NextJS 应用程序中运行博客的 Express 应用程序,与 example in their repo
非常相似我已将其设置为让我的应用运行查询以获取博客文章,如果结果为空,则会抛出 NotFoundException
.
我在我的 NextJS _error.js
文件中捕获了这个异常,它类似于 React 错误边界,我在其中将用户路由到我的 404 页面。这部分工作正常。
我遇到的问题是这个异常被记录到节点控制台,即使我在捕获异常时没有记录它。这会用我们所有的 404
污染我们公司的日志记录软件这里是否缺少一些 node/express 设置来防止记录异常?这是我的 Express 进程错误处理程序:
process.on('unhandledRejection', (reason, promise) =>
console.error(`Unhandled Rejection at: ${promise}.\nreason: ${reason.stack || reason}`));
我知道那里有一个日志,但我要删除的格式与此不同,所以我确信这不是来源。
在 express 中,您可以设置一个 ErrorMiddleware。 在你所有的路线声明之后,把
server.use(function(req, res, next) {
handler(req, res).catch(e => {
// use rejected promise to forward error to next express middleware
next(e)
})
});
像这样,当你拒绝一个 Promise 时,next(e) 会将你的错误发送给下一个中间件。我通常在发送错误的地方设置一个中间件,然后我在一个函数中管理所有错误(基于 statusCode 错误,...)。
我不会假装知道发生了什么,但我最好的猜测是 next.js 正在某处记录错误。我做了一些挖掘,看起来服务器代码中有一个错误记录器,它将记录错误,除非在服务器上设置 quiet
属性:
https://github.com/zeit/next.js/blob/canary/packages/next-server/server/next-server.ts#L105:
return this.run(req, res, parsedUrl)
.catch((err) => {
this.logError(err)
res.statusCode = 500
res.end('Internal Server Error')
})
这是 logError 函数的签名和正文:
private logError(...args: any): void {
if (this.quiet) return
// tslint:disable-next-line
console.error(...args)
}
如果您查看有关将下一个 API 与自定义服务器一起使用的文档,它会指出可以传递给构造函数的以下选项对象属性:
The next API is as follows:
next(opts: object) Supported options:
dev (bool) whether to launch Next.js in dev mode - default false
dir (string) where the Next project is located - default '.'
quiet (bool) Hide error messages containing server information - default false
conf (object) the same object you would use in next.config.js - default {}
在构造下一个对象时,尝试将 quiet 传递为 true 以查看是否可以解决您的问题:
const express = require('express')
const next = require('next')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev, quiet: true })
const handle = app.getRequestHandler()
文档还提到在非生产环境中记录错误(在 process.env.NODE_ENV !== 'production'
时识别),因此我也会检查以确保您将 NODE_ENV 设置为 'production'开始申请:
NODE_ENV=production node server.js
希望对您有所帮助!