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,
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
Installing Boom
You can install boom by running the below command.
$
Once it is installed, you can use it in
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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.