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

extendable-http-errors

v1.1.4

Published

Extendable http error for node server using es2015, covers most HTTP default errors and allow extending them for custom errors.

Downloads

103

Readme

#Extendable Http Errors

Version npm

NPM

##Introduction

Error creator for creating custom made and common HTTP errors for node application. Each http error contains the http status. The user can define the error code for the application to identify.

##Implemented Http Errors

  • BadRequestError
  • ForbiddenError
  • ImATeapot
  • InternalServerError
  • NotFoundError
  • NotImplementedError
  • ServiceUnAvailable
  • UnauthorizedError

##Installation

npm install extendable-http-errors --save

##Code Samples

###Use specific http error:

const httpErrors = require('extendable-http-errors').httpErrors;

//No need to change status it got the default type of http error status
function (req, res, next) {    
    let errMsg = "Error Route Not Found";
    let section = "Routes";
    let code = 9404; //error code for the client to identify the error
    let params = {url: req.url};
    
    let err = new httpErrors.NotFoundError(errMsg, section, code, params);
    next(err);
}

Back to top

###Make all errors global

require('extendable-http-errors').initGlobalErrors();

console.log(new InternalServerError("Internal Server Error", "Core", 9500)); 
InternalServerError: Interl Server Error
  at /Users/Username/WebstormProjects/Project/app.js:12:15
  at Layer.handle [as handle_request] (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/layer.js:95:5)
  at next (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/route.js:131:13)
  at Route.dispatch (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/route.js:112:3)
  at Layer.handle [as handle_request] (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/layer.js:95:5)
  at /Users/Username/WebstormProjects/Project/node_modules/express/lib/router/index.js:277:22
  at Function.process_params (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/index.js:330:12)
  at next (/Users/Username/WebstormProjects/Project/node_modules/express/lib/router/index.js:271:10)
  at expressInit (/Users/Username/WebstormProjects/Project/node_modules/express/lib/middleware/init.js:33:5)
code: 9500,
section: 'Core',
uuid: '8a77fc8b-601c-4fbb-89df-570ced4fa42b'

Back to top

###Custom http error

Before changing the 'this', need to call the super method so the new extended error could be changed.

const extendableHttpErrors = require('extendable-http-errors');

class BadLoginRequest extends extendableHttpErrors.httpErrors.BadRequestError {
    constructor(msg, section, code, reason){
        reason = reason.toUpperCase();
        super(msg, section, code, {reason: reason});
    }
}

const customErrors = {
    BadLoginRequest
};

extendableHttpErrors.initGlobalErrors(customErrors);

console.log(new BadLoginRequest("Bad Login Request!", "SpecialLogin", 9876, "User is not special enough"));

Back to top

###Convert error to object to send to the client

const extendableHttpErrors = require('extendable-http-errors');

const app = express();
const customErrors = {
    CustomError: require('./errors/custom-error')
};

extendableHttpErrors.initGlobalErrors(customErrors);

app.use(extendableHttpErrors.prettifyErrorMiddleware);

app.get('/', function (req, res, next) {
    let err = new CustomError("Custom Error!!", "Custom", 9876, "User is not custom enough");
    next(err);
});

//Error Handler
app.use(function (err, req, res, next) {
    res.status(err.status).json(err);
});

Back to top

###Convert error to object

const extendableHttpErrors = require('extendable-http-errors');

const app = express();
const customErrors = {
    CustomError: require('./errors/custom-error')
};

extendableHttpErrors.initGlobalErrors(customErrors);

app.get('/', function (req, res, next) {
    let err = new CustomError("Custom Error!!", "Custom", 9876, "User is not custom enough");
    next(err);
});

//Error Handler
app.use(function (err, req, res, next) {
    err = extendableHttpErrors.prettifyErrorFunction(err);
    res.status(err.status).json(err);
});

###Wrap if not wrapped unknown error

If the error is already created from the extendable-http-errors it will return it self, otherwise it will create new extendable-http-errors Error with the message of the new error.

const extendableHttpErrors = require('extendable-http-errors');

const app = express();
const customErrors = {
    CustomError: require('./errors/custom-error')
};

extendableHttpErrors.initGlobalErrors(customErrors);

app.use(extendableHttpErrors.prettifyErrorMiddleware);

app.get('/', function (req, res, next) {
    let err = new CustomError("Custom Error!!", "Custom", 9876, "User is not custom enough");
    next(err);
});

//Error Handler
app.use(function (err, req, res, next) {
    //If error is already created from the extendable-errors,
    //it will throw it self otherwise it will wrap it with bad request error.
    res.status(err.status).json(BadRequestError.wrapIfNotWrapped(err)); 
});

Back to top