Handling HTTP Error Status Code With Boom In Node

Posted By : Hotam Singh | 28-Apr-2018

Consider a scenario, we are using microservices in NodeJS application and each microservice is handled by the separate team. Each team using some standards to handle error codes.

 

In this case, all teams must handle one or more microservices that communicate with each other via some sort of HTTP protocols. In this scenario, handling error code matters a lot. 

 

To handle this scenario, what we can do is, we need to define some sort of standard across all the Microservices. Every microservice needs to follow these standards in order to handle consistent integration with the user interface or in the mobile application.

 

In this article, We'll be exploring you a tool. By using this module you can standardize the HTTP status code across all of the microservices.

 

Let's start coding.

 

Handling HTTP Status Code in Node


Who are familiar with NodeJS, they have seen handling status code in NodeJS like given below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
const express = require('express');
const app = express();
const router = express.Router();

router.get('/', (req,res) => {
  res.send('OK'); // Sends HTTP status code 200 back to browser
});

//Set all request as invalid except root request
router.all('*',(req,res) => {
  res.status(404).send('404 Invalid Request');
});

app.use('/',router);

app.listen(process.env.port || 3000);

 

The code above is simple NodeJS code, In response, server sends status codes. In above example, I have given example of 200(Ok) and 404(Not Found) only. We can further process front-end based on responses returned from server.

 

We are going to handle requests with status code 200 and 404, but there are various HTTP requests with different status codes assigned for a purpose.

 

There are many status code which we encounter generally:

 

For example: HTTP status code:

 

200 - ok
201 - created
204 - No content
400 - bad request
401 - Unauthorized access
408 - client timeout and so on.

 

To handle all of these status codes, there is a module called Boom which might help you to define same standardize rule for your NodeJS appliaction.

 

Installing Boom


You can install boom by running the below command.

 

$ npm i --S boom
 

Once it is installed, you can use it in you application wherever you need and send the HTTP responses

 

For example:
 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
const express = require('express');
const app = express();
const Boom = require('boom')
const router = express.Router();

router.get('/', (req,res) => {
  res.send('OK'); // Sends HTTP status code 200 back to browser
});

//handling invalid request with boom
router.all('*',(req,res) => {
  res.json(Boom.notFound('Invalid Request')); // this will set the status to 404
});

app.use('/',router);

app.listen(process.env.port || 3000);


 

Handling some other error codes:

 

403 Forbidden

Boom.forbidden([message], [data])

Returns a 403 Forbidden error where:

message - optional message.

data - optional additional error data.


1
2
3
4
//handling Forbidde request with boom
router.all('*',(req,res) => {
  res.json(Boom.forbidden('Invalid Request')); // this will set the status to 403
});

Generates the following response payload:

{
    "statusCode": 403,
    "error": "Forbidden",
    "message": "try again some time"
}

 

400 Bad Request



Boom.badRequest([message], [data])

Returns a 400 Bad Request error where:

 

1
2
3
4
//handling bad request with boom
router.all('*',(req,res) => {
  res.json(Boom.badRequest('Invalid Request')); // this will set the status to 400
});

Generates the following response payload:

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "invalid query"
}


You can find all of the HTTP status code and examples for each status code here https://github.com/hapijs/boom.

 

 

 

 

 

 

 

 

 

About Author

Author Image
Hotam Singh

Hotam has 1.5 years of experience in Node.JS. He has worked on Front End, JavaScript, Jquery, Backbone.JS, Database: MySQL, MongoDB. His hobbies are playing Sudoku/Puzzles and watching movies.

Request for Proposal

Name is required

Comment is required

Sending message..