In Koa, error handling is done using try/catch (except with event emitters).
You might not have seen this in a while if you’ve been working with
Express and most other node frameworks.
Unlike, Express, error handlers are simply decorators that you add to the top of your middleware stack.

  1. app.use(function* errorHandler(next) {
  2. try {
  3. // catch all downstream errors
  4. yield next;
  5. } catch (err) {
  6. // do something with the error
  7. }
  8. })
  9. app.use(function* () {
  10. // throw an error downstream
  11. throw new Error('boom!');
  12. })

Each Koa app is an EventEmitter instance.
All errors uncaught by any middleware are caught by app.on('error', function (err, context) {}).
This is useful for logging.
However, if you create your own error handler (i.e. catching it),
you will have to manually emit these events yourself.

In a real app, you would only want to app.emit('error', err, this)
unknown errors. Client errors, such as validation errors,
don’t need to be logged.
Using an error handler also allows you to create your own error pages.

Exercise

Create an error handler that intercepts downstream errors,
responds to the client with internal server error,
and emits the error on app.