grupoarrfug.com

Effective Error Handling in Hapi.js Back-End Development

Written on

Chapter 1: Introduction to Hapi.js Error Handling

Hapi.js is a lightweight Node.js framework designed for building back-end web applications. In this guide, we will explore how to implement error handling effectively within your Hapi.js applications.

Throwing Unauthorized Errors

To generate unauthorized errors, we can utilize the @hapi/boom module. This module includes the Boom.unauthorized method for throwing errors. Consider the following example:

const Hapi = require('@hapi/hapi');

const Boom = require('@hapi/boom');

const init = async () => {

const server = new Hapi.Server({

port: 3000,

host: '0.0.0.0'

});

server.route({

method: 'GET',

path: '/',

handler(request, h) {

throw Boom.unauthorized('invalid password');

}

});

await server.start();

console.log('Server running at:', server.info.uri);

};

process.on('unhandledRejection', (err) => {

console.log(err);

process.exit(1);

});

init();

When we call the Boom.unauthorized method with a specific message, we will receive a 401 response as follows:

{"statusCode":401,"error":"Unauthorized","message":"invalid password"}

We can also include additional data in the third argument:

server.route({

method: 'GET',

path: '/',

handler(request, h) {

throw Boom.unauthorized('invalid password', 'sample', { ttl: 0, cache: null, foo: 'bar' });

}

});

The additional object will be added to the attributes of the response, resulting in:

{"statusCode":401,"error":"Unauthorized","message":"invalid password","attributes":{"ttl":0,"cache":"","foo":"bar","error":"invalid password"}}

The second argument appears in the Www-Authenticate header:

sample ttl="0", cache="", foo="bar", error="invalid password"

Video: Hapi Validation and Error Handling - This video provides a comprehensive overview of managing validation and errors in Hapi.js applications.

Payment Required Responses

To return a 402 response, we can use the Boom.paymentRequired method:

server.route({

method: 'GET',

path: '/',

handler(request, h) {

throw Boom.paymentRequired('bandwidth used');

}

});

This will yield:

{"statusCode":402,"error":"Payment Required","message":"bandwidth used"}

Forbidden Errors

For a forbidden error, the Boom.forbidden method can be employed:

server.route({

method: 'GET',

path: '/',

handler(request, h) {

throw Boom.forbidden('try again some time');

}

});

This results in the following response:

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

Not Found Errors

To generate a 404 error, we can utilize the Boom.notFound method:

server.route({

method: 'GET',

path: '/',

handler(request, h) {

throw Boom.notFound('missing');

}

});

The response will be:

{"statusCode":404,"error":"Not Found","message":"missing"}

Video: Error Handling in NodeJS (Complete Guide) - This tutorial covers best practices for error handling in Node.js applications, focusing on Hapi.js.

Conclusion

The @hapi/boom module simplifies the process of throwing various types of errors in Hapi.js, allowing developers to manage error responses effectively.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

The Truth About UFOs: A Journey Through Government Secrets

An exploration of UFO phenomena, government secrecy, and public fascination, questioning the implications of newfound revelations.

Effective Science Communication: The Who, Why, and How

Exploring the importance of effective science communication to enhance public understanding and engagement with scientific research.

Unlocking the Key to Self-Esteem: Transformative Habits

Explore three habits that undermine self-esteem and learn how to replace them for a healthier, happier life.