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

problem-details-http

v2.0.0

Published

return http errors as stated in the document Problem Details for HTTP APIs RFC7807

Downloads

697

Readme

problem-details-http

NPM Version License Last Commit

Open Issues Dependencies GZIP Size

Overview

Welcome to the documentation for the HTTP Problem Details Library. This library is designed to facilitate the implementation of the HTTP Problem Details convention as specified in RFC 7807. The library helps you structure and return error responses in a consistent and standardized format.

Installation

To use this library in your project, you can install it via npm. Run the following command in your project directory:

npm install problem-details-http

Intro

A problem details object looks like this:

{
  "type": "https://example.com/probs/out-of-credit",
  "status": 403,
  "title": "You do not have enough credit.",
  "detail": "Your current balance is 30, but that costs 50.",
  "instance": "/account/12345/msgs/abc"
}

When the members are:

  • type (string): URI to the explanation of the error type
  • status (number): HTTP status code
  • title (string): Short description of the error
  • detail (string): Specific explanation of the error
  • instance (string) (optional): URI to some instance where the error ocurred

This object also allows to store member extensions, e.g :

{
  "type": "https://example.com/probs/out-of-credit",
  "status": 403,
  "title": "You do not have enough credit.",
  "detail": "Your current balance is 30, but that costs 50.",
  "instance": "/account/12345/msgs/abc",
  "balance": 30,
  "accounts": ["/account/12345", "/account/67890"]
}
{
  "type": "https://example.net/validation-error",
  "status": 422,
  "title": "Your request is not valid.",
  "detail": "The are some validation errors in your request",
  "errors": [
    {
      "detail": "must be a positive integer",
      "pointer": "#/age"
    },
    {
      "detail": "must be 'green', 'red' or 'blue'",
      "pointer": "#/profile/color"
    }
  ]
}

Usage

Importing the Library

In your project, import the library as follows:

import { PDBuilder } from "problem-details-http";

Creating a Problem Details Object

To create a Problem Details object, you can use the fromDetail function, followed by the methods that are named the same as the members established in the document, this method is named fromDetail because the "detail" member should be the only required member that your application must specifically provide.

const problemDetails = PDBuilder.fromDetail("This is an example error message.")
  .type("https://example.com/error")
  .title("Example Error")
  .status(400)
  .instance("URI/to/the/instance")
  .build();

And this will create an object like this:

{
  "type": "https://example.com/error",
  "status": 400,
  "title": "Example Error",
  "detail": "This is an example error message.",
  "instance": "URI/to/the/instance"
}

Creating a Problem Details Object With Default Http Data

This library provides default values for all http errors in case you do not have a specific URI for your error type or u prefer to use generic titles.

const problemDetails = PDBuilder.fromDetail(
  "This is an example error message."
).build();

And this will create an object like the following one, 400 is the default status code:

{
  "type": "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1",
  "status": 400,
  "title": "Bad Request",
  "detail": "This is an example error message."
}

Returning Problem Details as JSON

You can then return the Problem Details object as a JSON response in your HTTP endpoint:

res.status(problemDetails.status).json(problemDetails);

Handling Errors

When an error occurs in your application, use the library to create and return a Problem Details object. This ensures that error responses adhere to the RFC 7807 standard. You can take advantage of the Error instance using the PDBuilder, it takes the constructor.name as the title, and the message property as the detail member

// OutOfCreditError.ts
export class OutOfCreditError extends Error {}
// exampleController.ts
try {
  // some code
  if (something.goesWrong()) {
    throw new OutOfCreditError(
      `Your current balance is ${current}, but that costs ${price}.`
    );
  }
} catch (error) {
  if (error instanceof OutOfCreditError) {
    const problemDetails = PDBuilder.fromError(error)
      .status403()
      .type("https://example.com/probs/out-of-credit")
      .instance("URI/to/the/instance")
      .extensions({
        balance: 30,
        accounts: ["/account/12345", "/account/67890"],
      });

    res.status(problemDetails.status).json(problemDetails);
  }
}

Response of the api endpoint

HTTP/1.1 403 Forbidden
Content-Type: application/problem+json
{
  "type": "https://example.com/probs/out-of-credit",
  "title": "Out of credit",
  "status": 403,
  "detail": "Your current balance is 30, but that costs 50.",
  "instance": "URI/to/the/instance",
  "balance": 30,
  "accounts": ["/account/12345", "/account/67890"]
}

Contributing

We welcome contributions to improve and expand this library. If you encounter issues or have suggestions, please open an issue or submit a pull request on our GitHub repository.

License

This library is distributed under the MIT License. Feel free to use, modify, and distribute it as needed.

Thank you for using the HTTP Problem Details Library!