npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@enjoys/exception

v1.1.2

Published

Enhance your Express.js applications with our Exception Handler package, a robust middleware solution for effective error management. Seamlessly handle various error types, from validation errors to server crashes, ensuring graceful responses to clients.

Downloads

149

Readme

Express.js Exception Filter/Handler

Express.js Exception Filter/Handler

Overview

The Express.js Exception Handler is a middleware package designed to streamline error management and enhance the reliability of your Express.js applications. It simplifies the process of handling errors, customizing error messages and status codes, and maintaining clean and consistent error-handling practices.

Features

  • Robust Error Handling: Efficiently manage various error types, from validation errors to unexpected server issues.
  • Customization: Easily customize error messages and HTTP status codes to provide meaningful responses to clients.
  • Integration: Seamlessly integrate error-handling middleware into your Express.js application stack.
  • Best Practices: Follow industry best practices for error handling to improve the professionalism and reliability of your application.

Installation

You can install the Express.js Exception Filter/Handler package via npm:

npm  i  @enjoys/exception

Usage

What is New ? Exceptions =>

  • createHandlers() // will return ExceptionHandler, UnhandledRoutes,CustomExceptionHandler
  • ServerErrorException
  • NotFoundException
  • UnAuthorizedException
  • BadGatewayException
  • DuplicateEntryException
  • ForbiddenException
  • PayloadTooLargeException
  • TooManyRequestException
  • MethodNotAllowedException
  • UnacceptableException
  • NotImplementedException
app.get("/my/route",()=>  new  ServerErrorException();
// constructor ServerErrorException({stack:<string or object>) , rest are same...

  1. Import the package: ES5 Syntax
const { HttpException} = require('@enjoys/exception');

ES6 Syntax

import {HttpException} from'@enjoys/exception';

To Handle Undefined Route throughout your App, use this:

import { createHandlers } from'@enjoys/exception'; // ES6 Syntax
// const { createHandlers } = require('@enjoys/exception'); // ES5 Syntax
const { ExceptionHandler, UnhandledRoutes,CustomExceptionHandler } = createHandlers();
//... your app code
app.use("*",UnhandledRoutes);
// call this at the last of all routes and before the (global/app) exception handler

To Handle Exception throughout your App, use this:

  1. Add the middleware to your Express.js application:
// call this function after routes has been called otherwise won't works
app.use(ExceptionHandler);
// OR
app.use(CustomExceptionHandler);
  1. Customize error handling and responses as needed in your routes and controllers.
routes.all("*",Exception.HttpException.ExceptionHandler)

Throw an Exception


new  HttpException({
name:"auto suggestions",// name is required. press ctrl + space
message:"<your-message>",
stack:"your stack must be string or a object"
})

//for auto suggestion press, ctrl+space
// More Examples

new  DuplicateEntryException({
name:"auto suggestions", // for all defined exception name and message are optional.
message:"<your-message>",
stack:"your stack must be string or a object"
})

Full Usage Example

const  express  =  require('express');
const  app  =  express();
const { ServerErrorException,createHandlers } =  require('@enjoys/exception');
const{ CustomExceptionHandler,UnhandledRoutes,ExceptionHandler }=  createHandlers()
app.get('/error', (req, res) => {
throw new  ServerErrorException({stack:{}})
});
 
app.use("*",UnhandledRoutes) // to Handle 404 or  Cannot GET /test
app.use(ExceptionHandler ) // Handle Error on Application Level,
app.listen(5200);

Contributing

We welcome contributions from the community. If you have ideas for improvements or encounter any issues, please open an issue or submit a pull request.

License

This project is licensed under the MIT License

If you have any questions or need assistance, feel free to contact us at [email protected].

Feel free to adapt this template to your specific project, including adding actual links, installation steps, and contact information.