防止Mongoose堆栈跟踪错误

时间:2016-03-08 01:34:11

标签: node.js mongoose

Mongoose为强制转换错误发出堆栈跟踪。我知道如何防止Mongoose错误 - 请不要回答如何防止错误。

如何在生产中阻止Mongoose发出堆栈跟踪错误?

  

错误:传入的参数必须是12个字节的单个字符串或a   新ObjectID中的24个十六进制字符的字符串   (C:\ PROJ \ fboapp \ node_modules \猫鼬\ node_modules \ BSON \ lib中\ BSON \ objectid.js:38:11)   在c:\ proj \ fboapp \ routes \ user \ no_auth_user_api_routes.js:135:27 at   Layer.handle [as handle_request]   (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ layer.js:95:5)at at   next(c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ route.js:131:13)   在Route.dispatch   (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ route.js:112:3)at at   Layer.handle [as handle_request]   (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ layer.js:95:5)at at   c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:277:22 at   Function.process_params   (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:330:12)at at   next(c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:271:10)   在Function.handle   (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:176:3)at at   router(c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:46:12)   在Layer.handle [as handle_request]   (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ layer.js:95:5)at at   trim_prefix   (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:312:13)at at   c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:280:7 at   Function.process_params   (c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:330:12)at at   next(c:\ proj \ fboapp \ node_modules \ express \ lib \ router \ index.js:271:10)

Nodejs v0.12.3 Mongoose v4.4.3

2 个答案:

答案 0 :(得分:2)

我很困惑为什么在没有错误处理程序的情况下将错误呈现给浏览器,直到我阅读ExpressJS error handling documentation page

显然有默认错误处理程序,在没有指定错误处理程序时触发。

  

Express附带一个内置的错误处理程序,可以处理任何错误   应用程序中可能遇到的错误。这个默认值   错误处理中间件功能在最后添加   中间件功能堆栈。

最佳做法是为生产指定自定义错误处理程序,该处理程序不会将堆栈跟踪输出到浏览器。默认错误处理程序始终将堆栈跟踪输出到浏览器。

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

自定义错误处理程序的一个示例:

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

答案 1 :(得分:2)

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

以下是代码中没有try-catch块的测试代码,然后阻止堆栈跟踪。请参阅此模块mongoose disable stack trace,同时将添加some new errors添加到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
相关问题