1.1.5 • Published 2 months ago

@rafikidota/http-exceptions v1.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

HTTP Exceptions

Just my own http exceptions library with blackjack and hookers

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

1.1.5

2 months ago

1.1.4

2 months ago

1.1.3

2 months ago

1.1.0

2 months ago

1.1.2

2 months ago

1.0.2

3 months ago

1.0.3

3 months ago

1.0.1

3 months ago

1.0.0

3 months ago

0.0.9

3 months ago

0.0.8

3 months ago

0.0.7

3 months ago

0.0.6

3 months ago

0.0.5

3 months ago