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/