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

json-api-error

v1.1.0

Published

JSON API Errors

Downloads

21

Readme

JSON API Errors

JSON API Errors is a collection of popular errors which is format under of JSON API Specification. More detailed in here

Getting Started

Installation

The easiest way to install json-api-error is using NPM. If you have Node.js installed, it is most likely that you have NPM installed as well.

$ npm install json-api-error

Usage

At the current implementation, json-api-error support the following common errors

  • BadRequestError
  • UnauthorizedError
  • MalformedError
  • ForbiddenError
  • NotFoundError
  • MethodNotAllowedError
  • NotAcceptableError
  • RequestTimeoutError
  • UnsupportedMediaTypeError
  • InternalError
  • NotImplementedError
  • BadGatewayError
  • ServiceUnavailableError
  • GatewayTimeoutError

Example

Error usage with simple detailed message

import { BadRequestError } from 'json-api-error';

throw new BadRequestError('Request is invalid');

Error usage with options

import { BadRequestError } from 'json-api-error';

throw new BadRequestError({
    id: 'BadRequestError',
    code: 'BadRequestError',
    title: 'BadRequestError',
    detail: 'The #/userName must be number'
}));

Error usage with customized your JsonApiError

import JsonApiError from 'json-api-error';

throw new JsonApiError({
    id: 'RequestEntityTooLargeError',
    status: '413',
    code: 'RequestEntityTooLargeError',
    title: 'RequestEntityTooLargeError',
    detail: 'Request Entity Too Large'
  });

AggregateJsonApiError(errors, status)

import { AggregateJsonApiError } from 'json-api-error';

throw new AggregateJsonApiError([
  new BadRequestError('Something went wrong'),
  new BadRequestError('Request is valid')
], 400);

Express Middleware - JSON API Error Handler usage

json-api-error also ships a Express middleware to handle these JSON API Errors. This middleware will catch JSON API Error and return response for your end-user as the following example format (including the status response):

{
  errors: [{
    id: 'NotFoundError',
    status: '404',
    code: 'NotFoundError',
    title: 'NotFoundError',
    detail: 'Resource was not found'
  }]
}

To use this, configure your Express app

import { jsonApiErrorHandler } from 'json-api-error/middlewares';


app.use(jsonApiErrorHandler);

Note: For best practice, please place jsonApiErrorHandler below utilized middlewares.

Options

| Properties | Detail | Type | Default Value | |---|---|---|---| | id | a unique identifier for this particular occurrence of the problem | string | equal to error name | | links | a links object, more detailed in here | object | N/A | | status | the HTTP status code applicable to this problem, expressed as a string value | string | equal to HTTP code | | code | an application-specific error code, expressed as a string value | string | equal to error name | | title | a short, human-readable summary of the problem | string | equal to error name | | detail | a human-readable explanation specific to this occurrence of the problem | string | equal to error name | | source | an object containing references to the source of the error, more detailed in here | object | N/A | | meta | a meta object containing non-standard meta-information about the error. | object | N/A |

LICENSE

MIT