Skip to content

rafikidota/http-exceptions

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published