@rafikidota/http-exceptions
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

HTTP Exceptions

Just my own http exceptions library with blackjack and hookers

Bender

npm

npm install @rafikidota/http-exceptions

yarn

yarn add @rafikidota/http-exceptions

pnpm

pnpm add @rafikidota/http-exceptions

Using http exceptions with Express

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}`);
});

HttpException

app.get('/http-exception', (req, res) => {
    throw new HttpException('Not Implemented', 501);
});

BadRequest

app.get('/bad-request', (req, res) => {
    throw new BadRequestException('An bad request example');
});

Unauthorized

app.get('/unauthorized', (req, res) => {
    throw new UnauthorizedException('An unauthorized example');
});

Forbidden

app.get('/forbidden', (req, res) => {
    throw new ForbiddenException('A forbidden example');
});

Not found

app.get('/not-found', (req, res) => {
    throw new NotFoundException('An not found example');
});

Interal Server Error

app.get('/internal-server-error', (req, res) => {
    throw new InternalServerErrorException('An internal server error example');
});

Error handling

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

Package Sidebar

Install

npm i @rafikidota/http-exceptions

Weekly Downloads

2

Version

1.2.0

License

MIT

Unpacked Size

29.3 kB

Total Files

6

Last publish

Collaborators

  • rafikidota