node.js - Koa.js : Centralized error handling

标签 node.js mongoose error-handling koa koa2

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

相关文章:

javascript - 视频到音频文件转换并通过 node js 中的 FFMPEG 保存

node.js - 在 Windows 7 上 npm install mongoose 失败

python - 如何处理Python中被调用库生成的弹出错误提示?

hibernate - GWT-合并错误消息以在服务器和客户端之间使用它们

asp.net-mvc - 哪个 .NET 框架用于 ASP.NET 中的复杂日志记录?

javascript - 为什么 Node.js querystring.parse() 返回一个不是从 JavaScript `Object` 继承的对象?

javascript - octokit getLabel 返回 TypeError : callback. 绑定(bind)不是函数

javascript - NodeJS : What is the correct way to use different folder?

javascript - 基于先前调用的响应的异步调用,避免回调 hell

javascript - 使用自定义 UID 设置 mongo 的 _id