【发布时间】:2018-12-27 10:52:08
【问题描述】:
我通过 Mongoose 使用 Koa.js 和 MongoDB。我想知道如何实现一个完美的集中式错误处理机制。
例如,我编写的通过 Mongoose 连接到 MongoDB 的函数存在一些问题。这就是为什么我收到以下错误。但是,我想捕获此错误并以集中方式处理它。意思是,我希望所有错误和警告(无论来自应用程序的哪个部分)都由我的应用程序中的一个函数处理,该函数将记录它。
warning.js:18 (node:11776) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: read ECONNRESET]
writeOut @ warning.js:18
output @ warning.js:69
process.on @ warning.js:100
emitOne @ events.js:116
emit @ events.js:211
(anonymous) @ warning.js:74
_combinedTickCallback @ next_tick.js:131
_tickCallback @ next_tick.js:180
TickObject (async)
init @ inspector_async_hook.js:22
emitInitNative @ async_hooks.js:472
emitInitScript @ async_hooks.js:388
nextTick @ next_tick.js:270
process.emitWarning @ warning.js:146
emitWarning @ promises.js:75
emitPendingUnhandledRejections @ promises.js:95
_tickCallback @ next_tick.js:189
Show 11 more blackboxed frames
warning.js:18 (node:11776) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
writeOut @ warning.js:18
output @ warning.js:69
process.on @ warning.js:100
emitOne @ events.js:116
emit @ events.js:211
(anonymous) @ warning.js:74
_combinedTickCallback @ next_tick.js:131
_tickCallback @ next_tick.js:180
TickObject (async)
init @ inspector_async_hook.js:22
emitInitNative @ async_hooks.js:472
emitInitScript @ async_hooks.js:388
nextTick @ next_tick.js:270
process.emitWarning @ warning.js:146
emitWarning @ promises.js:78
emitPendingUnhandledRejections @ promises.js:95
_tickCallback @ next_tick.js:189
Show 11 more blackboxed frames
warning.js:18 (node:11776) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: read ECONNRESET]
writeOut @ warning.js:18
output @ warning.js:69
process.on @ warning.js:100
emitOne @ events.js:116
emit @ events.js:211
(anonymous) @ warning.js:74
_combinedTickCallback @ next_tick.js:131
_tickCallback @ next_tick.js:180
TickObject (async)
init @ inspector_async_hook.js:22
emitInitNative @ async_hooks.js:472
emitInitScript @ async_hooks.js:388
nextTick @ next_tick.js:270
process.emitWarning @ warning.js:146
emitWarning @ promises.js:75
emitPendingUnhandledRejections @ promises.js:95
_tickCallback @ next_tick.js:189
Show 11 more blackboxed frames
【问题讨论】:
标签: node.js mongoose error-handling koa koa2