我正在通过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

最佳答案

Koa Wiki中有一个有关错误处理的部分:https://github.com/koajs/koa/wiki/Error-Handling

他们建议使用错误处理程序中间件作为应用程序中的第一批中间件之一,以便向下游抛出的任何错误都会冒出来。

他们在Wiki上的示例:

app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    ctx.status = err.status || 500;
    ctx.body = err.message;
    ctx.app.emit('error', err, ctx);
  }
});

app.on('error', (err, ctx) => {
  /* centralized error handling:
   *   console.log error
   *   write error to log file
   *   save error and request information to database if ctx.request match condition
   *   ...
  */
});

关于node.js - Koa.js : Centralized error handling,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51420298/

10-12 17:28