Error handling is one of the most important aspects of any application. Just like everything else in KoaJS, it uses middleware for error handling as well. In this post, we will look at Koa JS Error Handling with examples.

If you are new to KoaJS, I will recommend you to start with the post on getting started with KoaJS.

With regards to error handling, KoaJS supports two approaches. We can emit an error and then, listen to the error event to take some action such as log the error. On the other hand, we can also throw an error and then, catch the error to log it.

Let us look at both the approaches one-by-one.

1 – Koa JS Error Handling using Error Event

In this approach, we use the app.emit() and app.on() methods to perform error handling.

Look at the below example:

const koa = require('koa');
const app = new koa();

app.use(async (ctx, next) => {
  try {
    await Promise.reject('Something went wrong');
  } catch (err) {
    ctx.status = err.status || 500;
    ctx.body = err.message;
    ctx.app.emit('error', err, ctx);
  }
});
app.on('error', (err, ctx) => {
  console.log(err);
});


app.listen(3000, function(){
   console.log('Server running on https://localhost:3000')
});

Here, we first add a middleware using app.use() method. Within the try catch block, we reject a Promise to deliberately create an error situation.

Once we catch the error, we simply set the status code and also update the ctx.body with the error message from the promise rejection. Next, we call app.emit() method to emit the error event with the event name as error.

To listen to the event, we use the app.on() method. The first argument is the event name. Within the callback, we simply the log the error message to the console.

If we run the application and visit http://localhost:3000, we will see the error message ‘Something went wrong’ printed on the console.

2 – KoaJS Error Handling using ctx.throw

This is the second approach to error handling in Koa JS. Here, we basically use the the ctx.throw method.

See below example:

const koa = require('koa');
const app = new koa();

app.use(async (ctx, next) => {
  ctx.throw(500, 'error');
});
app.on('error', (err, ctx) => {
  console.log(err);
});

app.listen(3000, function(){
   console.log('Server running on https://localhost:3000')
});

In this case, we call the ctx.throw() method with the error code 500 and error message.

This error message will be available in the callback function that we pass to the app.on() method. Currently, we simply log the message to the console. In a more realistic application, you could manage the logging part any way you want.

Conclusion

With this, we have seen two ways of performing error handling in KoaJS. Both the ways are equally robust and the choice depends on the application developers. It is ideal to choose one approach and stick to it for a particular application.

Want to learn more about KoaJS? Check out this post on how to create a KoaJS Route Handler.

If you have any queries or comments about this post, please feel free to mention them in the comments section below.

Categories: BlogKoaJS

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *