node.js - 防止 Mongoose 堆栈跟踪错误

标签 node.js mongoose

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

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

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters at new ObjectID (c:\proj\fboapp\node_modules\mongoose\node_modules\bson\lib\bson\objectid.js:38:11) at 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 next (c:\proj\fboapp\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (c:\proj\fboapp\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (c:\proj\fboapp\node_modules\express\lib\router\layer.js:95:5) 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 next (c:\proj\fboapp\node_modules\express\lib\router\index.js:271:10) at Function.handle (c:\proj\fboapp\node_modules\express\lib\router\index.js:176:3) at router (c:\proj\fboapp\node_modules\express\lib\router\index.js:46:12) at Layer.handle [as handle_request] (c:\proj\fboapp\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (c:\proj\fboapp\node_modules\express\lib\router\index.js:312:13) 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 next (c:\proj\fboapp\node_modules\express\lib\router\index.js:271:10)

Nodejs v0.12.3 Mongoose v4.4.3

最佳答案

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

这是我的测试代码,代码中没有 try-catch block ,然后阻止堆栈跟踪。引用这个模块mongoose禁用堆栈跟踪,同时添加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

关于node.js - 防止 Mongoose 堆栈跟踪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35857356/

相关文章:

node.js - 如何跟踪 Mongoose 查询执行时间

javascript - Node.js Express 和 Mongoose,在 save() 之后渲染

node.js - 在 execFileSync 中传递选项对于 ffmpeg 失败

Javascript,运行函数 0.5 秒然后停止(循环)

javascript - 使用 nodeJS 查询前 5 个查看项目(排序)的 Mongoose

javascript - 如何处理Node.js中未处理的异常

node.js - Mongoose最新版本如何根据Date字段进行查询?

node.js - Mongoose + Typescript -> 导出模型接口(interface)

node.js - 添加到 Node.js 快速 session 的变量在下一个请求中未定义

mysql - Express - Sequelize - 在查询中添加外表字段