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

client-errors

v0.9.2

Published

Client error class for Node servers

Downloads

1,798

Readme

client-errors

Client (4xx) error classes including status codes and messages for each error in Node server.

Installation

$ npm install client-errors

Example

Basic Usage

const { ClientError, UnauthorizedError } = require('client-errors');

// Use of a derived class
throw new UnauthorizedError(); // default message: 'The user is not authorized'
throw new UnauthorizedError('you are a bad guy!'); // custom message: 'you are a bad guy'

// Use of a error code
throw new ClientError(401); // default message: 'The user is not authorized'
throw new ClientError(401, 'you are a bad guy!'); // custom message: 'you are a bad guy'

Usage of Sending Status Code

const express = require('express');
const router = express.Router();
const { ClientError, UnauthorizedError, BadRequestError } = require('client-errors');
const mongoose = require('mongoose');

router.put('/items/:id', updateItem);

function updateItem(req, res) {
  mongoose
    .model('Item')
    .findById(req.params.id)
    .then(item => {
      if (!item) throw new BadRequestError('item does not exist');
      if (item.ownedBy !== req.user.id) throw new UnauthorizedError('invalid access to this item');

      const data = req.body;
      if (data.ownedBy !== req.user.id) throw new ClientError(403, 'cannot update owners of items');

      item.set(data);
      item.save().then(res.json);
    })
    .catch(err => {
      let status, message;
      if (err.statusCode) {
        status = err.statusCode;
        message = err.message;
      } else {
        status = 422;
        message = err;
      }
      res.status(status).send({ message });
    });
}

Client Errors

| Code | Description | Code Name | Default Message | | ---- | ------------------------------- | --------------------------------- | ---------------------------------------------------------------------------------------------------------- | | 400 | Bad Request | BadRequestError | The server cannot process the request due to a client error | | 401 | Unauthorized | UnauthorizedError | The user is not authorized | | 403 | Forbidden | ForbiddenError | The server refused to authorize the request | | 404 | Not Found | NotFoundError | The server did not find a current representation for the target resource | | 405 | Method Not Allowed | MethodNotAllowedError | The method received is not allowed | | 406 | Not Acceptable | NotAcceptableError | The request is not acceptable to the user agent | | 407 | Proxy Authentication Required | ProxyAuthRequiredError | The client needs to authenticate itself in order to use a proxy | | 408 | Request Timeout | RequestTimeoutError | The request was not completed in the expected time | | 409 | Conflict | ConflictError | The request was not completed due to a conflict with the target resource | | 410 | Gone | GoneError | The target resource is no longer available at the origin server | | 411 | Length Required | LengthRequiredError | The server refuses to accept the request without a defined Content-Length | | 412 | Precondition Failed | PreconditionFailedError | One or more conditions given in the request header fields evaluated to false | | 413 | Payload Too Large | PayloadTooLargeError | The request payload is too large | | 414 | Request-URI Too Long | UriTooLongError | The request target is too longer | | 415 | Unsupported Media Typ | UnsupportedMediaTypeError | The payload is in a format not supported | | 416 | Requested Range Not Satisfiable | RequestedRangeNotSatisfiableError | None of the ranges in the request's Range header field overlap the current extent of the selected resource | | 417 | Expectation Failed | ExpectationFailedError | The expectation given in the request's Expect header field could not be met | | 418 | I'm a teapot | TeapotError | I'm a teapot | | 421 | Misdirected Request | MisdirectedRequestError | The request was directed at a server that is not able to produce a response | | 422 | Unprocessable Entity | UnprocessableEntityError | The server is unable to process the request | | 423 | Locked | LockedError | The source or destination resource of a method is locked | | 424 | Failed Dependency | FailedDependencyError | The requested action depended on another action | | 426 | Upgrade Required | UpgradeRequiredError | This service requires use of a different protocol | | 428 | Precondition Required | PreconditionRequiredError | This request is required to be conditional | | 429 | Too Many Requests | TooManyRequestsError | The user has sent too many requests in a given amount of time | | 431 | Request Header Fields Too Large | RequestHeaderFieldsTooLargeError | Request header fields too large | | 451 | Unavailable For Legal Reasons | UnavailableForLegalReasonsError | Denied access due to a consequence of a legal demand |

MIT Licensed