title: Koa.js: Centralized error handling
body: <p>I'm using Koa.js and MongoDB via Mongoose. I'm want to know how to implement a perfect centralized error handling mechanism.</p>
  
  <p>For instance, there is some issue in the function that I've written to connect to  MongoDB via Mongoose. Which is why I'm getting the following error. However, I want to capture this error and handle it in a centralized fashion. Meaning, I want all the errors and warning (no matter from which part of the application) to be handled by a function in my application which will log it. </p>
  
  <pre><code>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
  </code></pre>