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

node-api-problem

v1.0.0

Published

Http Api Problem Utility

Downloads

571

Readme

node-api-problem NPM version Build Status Dependency Status Coverage percentage

Http Api Problem Utility

Installation

$ npm install --save node-api-problem

Why use Api Problem

When you developed some Rest Api, you commonly have to return error. Api Problem (following the #rfc7807) defined a way to carry machine-readable details of errors in a HTTP response to avoid the need to define new error response formats for HTTP APIs.

Api Problem should be compliant with the #rfc7807. The main definition of an Api Problem is defined in the #rfc7807 like that :


A problem details object can have the following members:

   o  "type" (string) - A URI reference [RFC3986] that identifies the
      problem type.  This specification encourages that, when
      dereferenced, it provide human-readable documentation for the
      problem type (e.g., using HTML [W3C.REC-html5-20141028]).  When
      this member is not present, its value is assumed to be
      "about:blank".

   o  "title" (string) - A short, human-readable summary of the problem
      type.  It SHOULD NOT change from occurrence to occurrence of the
      problem, except for purposes of localization (e.g., using
      proactive content negotiation; see [RFC7231], Section 3.4).

   o  "status" (number) - The HTTP status code ([RFC7231], Section 6)
      generated by the origin server for this occurrence of the problem.

   o  "detail" (string) - A human-readable explanation specific to this
      occurrence of the problem.

   o  "instance" (string) - A URI reference that identifies the specific
      occurrence of the problem.  It may or may not yield further
      information if dereferenced.

The #rfc precise another important point :

Problem type definitions MAY extend the problem details object with additional members.

If you use Api Problem, you will able to create custom Api Problem and precise any members you need and transform the "MAY" to a "MUST". This can be very useful if you have to reuse the same Api Problem in different circumstances.

One of the other interesting point of Api Problem is that it extend the Error object that let you the ability to have a full documented error if you need.

Limitation

The instance parameter is not yet implemented.

Usage

var ApiProblem = require('node-api-problem');

var Issue12Problem = new ApiProblem('http://api.acme.com/kb/issues/12','Error documented by issue 12');

Create an Api Problem on the Fly

var Issue12Problem = new ApiProblem('http://api.acme.com/kb/issues/12','Error documented by issue 12');

// or simply

var Issue12Problem = new ApiProblem('http://api.acme.com/kb/issues/12');

When you create an Api Problem on the fly, you will create internally a new Problem Type. This can be check easily :

var Issue12Problem = new ApiProblem('http://api.acme.com/kb/issues/12','Error documented by issue 12');
ApiProblem.lookupProblemType('http://api.acme.com/kb/issues/12'); // true

Use a pre-registered HTTP Api Problem

var GoneProblem = ApiProblem.GONE;

Create a Custom Api Problem

var IssueApiProblem = ApiProblem.create();
var Issue12Problem = new IssueApiProblem('http://api.acme.com/kb/issues/12');

The creation of Custom Api Problem is very useful because you can add custom variable in a specific namespace without denatured the root Api Problem.

var CustomApiProblem = ApiProblem.create();

var ValidationProblem = function(validation) {
  if(validation === 12) {
    validation = 42;
  }
  return new CustomApiProblem('http://api.acme.com/doc/validations', {'validation' : validation});
};

var validationIssue = new ValidationProblem(12);
echo validationIssue.validation; // 42

Add Custom Type to Api Problem

Pre-registered Type is good. We planned in the future the ability to lock the creation of Problem Type on the fly.

ApiProblem.registerProblemType('http://api.acme.com/kb/issues/12','Error documented by issue 12');
ApiProblem.lookupProblemType('http://api.acme.com/kb/issues/12'); // true

Throw an Api Problem

ApiProblem inherit from Error, you can easily throw a rich Api Problem using throw :

try {
    throw ApiProblem.GONE;
} catch (e) {
    e instanceOf ApiProblem; //true
    e instanceOf Error; //true
}

Cast Api Problem

An Api Problem can be cast to string :

String(ApiProblem.GONE); // "HTTP-Problem: [410] Gone > http://www.iana.org/assignments/http-status-codes#410"

An Api Problem can either be cast to a JSON string :

JSON.stringify(ApiProblem.GONE); // {\"type\":\"http://www.iana.org/assignments/http-status-codes#410\",\"title\":\"Gone\",\"status\":410}

Use Api Problem in a middleware

function apiProblemMiddleware(err,req,res,next) {
  if (err instanceof ApiProblem) {
    err.send(res);
  } else {
    next();
  }
}

License

MIT © Romain DARY <[email protected]> http://eoko.fr