防止Mongoose堆栈跟踪错误

Mongoose发出一个堆栈跟踪,以便发现一个转换错误。 我知道如何防止mongoose错误 – 请不要回答如何防止错误。

如何停止生产中的Mongoose发射堆栈跟踪错误?

错误:传入的参数必须是新的ObjectID(c:\ proj \ fboapp \ node_modules \ mongoose \ node_modules \ bson \ lib \ bson \ objectid.js:38:11)中的一个12字节的string或24个hex字符的string)在c:\ proj \ fboapp \ routes \ user \ no_auth_user_api_routes.js:135:27在Layer.handle [as handle_request](c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ layer.js:95: 5)在Route.dispatch(c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ route)下一步(c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ route.js:131:13)在c:\ proj \ fboapp \ node_modules \ express \目录下的Layer.handle [作为handle_request](c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ layer.js:95:5) (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:330:12)下的lib \ router \ index.js:277:22 (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:176:3)在路由器(c:\ proj \ express \ lib \ router \ index.js:271:10) \ fboapp \ node_modules \expression\ lib中\路由器\ index.js:46:12) 在c:\ proj \ fboapp \ node_modules \ express \ lib \ router \下使用Layer.handle [作为handle_request](c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ layer.js:95:5)在c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:280:7在Function.process_params(c:\ proj \ fboapp \ node_modules \ express \ lib \ router (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:271:10)

Nodejs v0.12.3 Mongoose v4.4.3

一般来说,在代码中添加try-catch块可能是正确的做法。

这里是我的代码没有try-catch块的testing代码,然后防止堆栈跟踪。 参考这个模块mongoose disable stack trace ,还添加一些新的错误被添加到mongoose,并将Error.stackTraceLimit设置为0将禁用堆栈跟踪收集。


index.js

 const captureStackTrace = Error.captureStackTrace; const CastError = module.parent.require('mongoose/lib/error/cast'); const VersionError = module.parent.require('mongoose/lib/error/version'); const ValidatorError = module.parent.require('mongoose/lib/error/validator'); const ValidationError = module.parent.require('mongoose/lib/error/validation'); const OverwriteModelError = module.parent.require('mongoose/lib/error/overwriteModel'); const MissingSchemaError = module.parent.require('mongoose/lib/error/missingSchema'); const DivergentArrayError = module.parent.require('mongoose/lib/error/divergentArray'); Error.captureStackTrace = function( that, params) { if(that instanceof CastError || that instanceof VersionError || that instanceof ValidatorError || that instanceof ValidationError || that instanceof OverwriteModelError || that instanceof MissingSchemaError || that instanceof DivergentArrayError) { Error.stackTraceLimit = 0; } else if (typeof that !== 'undefined'){ captureStackTrace.apply(Error, arguments); } } Error.captureStackTrace(new VersionError); 

app.js

 require('mongoose-disable-stack-trace'); var f = Foo({_id: new ObjectId('adss112'), key: '123'}); // invalid ObjectId 

输出:

 Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters c:\share\node\node_modules\mongoose\node_modules\mongodb\lib\server.js:283 process.nextTick(function() { throw err; }) ^ Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters 

我很困惑,为什么在没有error handling程序的情况下错误呈现给浏览器,直到我阅读ExpressJSerror handling文档页面 。

显然有一个默认的error handling程序 ,当没有指定error handling程序时触发。

Express附带了一个内置的error handling程序,该程序负责处理应用程序中可能遇到的任何错误。 这个默认的error handling中间件function被添加到中间件function栈的末尾。

最佳做法是为生产指定一个自定义error handling程序,该程序不会将堆栈跟踪输出到浏览器。 默认error handling程序总是将堆栈跟踪输出到浏览器。

没有必要使用try-catch块将未捕获的错误路由到自定义error handling程序,因为Express会自动将未捕获的错误路由到error handling程序。 另请注意:error handling程序中间件必须指定所有4个参数: err, req, res and next

自定义error handling程序的示例:

 app.use(function(err, req, res, next) { res.send('uncaught error in production'); });