Just my own http exceptions library with blackjack and hookers
npm install @rafikidota/http-exceptions
yarn add @rafikidota/http-exceptions
pnpm add @rafikidota/http-exceptions
The following JavaScript code snippet demonstrates an example of using the http exceptions library in conjunction with the Express framework. This code sets up a basic Express server and showcases how to handle HTTP exceptions using the library.
const express = require('express');
const { HttpException, BadRequestException, UnauthorizedException, ForbiddenException, NotFoundException, InternalServerErrorException } = require('@rafikidota/http-exceptions');
const app = express();
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
app.get('/http-exception', (req, res) => {
throw new HttpException('Not Implemented', 501);
});
app.get('/bad-request', (req, res) => {
throw new BadRequestException('An bad request example');
});
app.get('/unauthorized', (req, res) => {
throw new UnauthorizedException('An unauthorized example');
});
app.get('/forbidden', (req, res) => {
throw new ForbiddenException('A forbidden example');
});
app.get('/not-found', (req, res) => {
throw new NotFoundException('An not found example');
});
app.get('/internal-server-error', (req, res) => {
throw new InternalServerErrorException('An internal server error example');
});
app.use((error, req, res, next) => {
const { name, message, status } = error;
return res.status(status || 500).json({ name, message, status });
});
Explore the code example on GitHub here