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

bad-request-error

v1.0.2

Published

Custom error for HTTP request

Downloads

7

Readme

bad-request-error

BadRequestError is a custom error constructor for HTTP errors (bad parameters, forbidden, unauthorized…). It takes an error message as first parameter and optionally a HTTP error code as second parameter (defaults to 400).

Installation

It requires at least Node V6 as it uses JavaScript classes.

npm install --save bad-request-error

Usage

In addition to the native Error constructor properties, BadRequestError adds httpStatus and has its name property set to BadRequestError.

const BadRequestError = require('bad-request-error');

async function addComment(req, res) {
    await const isCorrectPasswd = checkPasswd(req);
    if (!isCorrectPasswd) return throw new BadRequestError('Your password looks wrong', 401);

    // whatever you have to do if password is correct
}

It's very convenient to use it with an error handler function like the following:

// /lib/handleError.js

/**
 * Send client err if it's validation error or log if it's a native or low level error
 * @param { Object } err error object
 * @param { Object } res
 */
function handleError(err, res) {
    // send errmsg to user if it's a BadRequestError
    if (res && err.name && err.name === 'BadRequestError') {
        res.status(err.httpStatus).json({ error: err.message });
        return;
    }

    // send http err if res object is provided
    if (res) res.status(500).send('Server Error');

    // if it's more low level, or if errorField isn't an error's propt
    console.error(err); // or custom logger like winston
}

module.exports = handleError;

If using promises, the error is easily handled in a catch block.

const BadRequestError = require('bad-request-error');
const handleError = require('/lib/handleError');

// this function return a promise with the same BadRequestError as above if password doesn't match
// BadRequestError('Your password looks wrong', 401);
function checkPasswd(passwd) {}

function addComment(req, res) {
    checkPasswd(req.body.passwd)
    .then(() => {
        // whatever you have to do if password is correct
    })
    .catch(err => handleError(err, res));
}

Contributing

There's sure room for improvement, so feel free to hack around and submit PRs! Please just follow the style of the existing code, which is Airbnb's style with minor modifications.

To maintain things clear and visual, please follow the git commit template.