JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3552
  • Score
    100M100P100Q126131F
  • License ISC

HTTP Problem Utility

Package Exports

  • api-problem

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (api-problem) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

api-problem version License

HTTP Problem Utility

Build Status Downloads Code Climate Coverage Status Dependencies

Install

npm install --production --save api-problem

API

Constructor: Problem(status[, title][, type][, members])

name type required default description ref
status String N/A The HTTP status code generated by the origin server for this occurrence of the problem draft-ietf-appsawg-http-problem, Section 3.1
title String HTTP status phrase* A short, human-readable summary of the problem type draft-ietf-appsawg-http-problem, Section 3.1
type String about:blank A URI reference that identifies the problem type draft-ietf-appsawg-http-problem, Section 3.1
details Object N/A additional details to attach to object draft-ietf-appsawg-http-problem, Section 3.1
import Problem from 'api-problem'

// HTTP defaults
new Problem(404) 
//=> { status: '404', title: 'Not Found', type: 'http://www.iana.org/assignments/http-status-codes#404' }


// override defaults
new Problem(404, 'Oops! Page Not Found') 
//=> { status: '404', title: 'Oops! Page Not Found', type: 'http://www.iana.org/assignments/http-status-codes#404' }


// custom values
new Problem(403, 'You do not have enough credit', 'https://example.com/probs/out-of-credit') 
//=> { status: '403', title: 'You do not have enough credit', type: 'https://example.com/probs/out-of-credit' }


// additional details
new Problem(403, 'You do not have enough credit', 'https://example.com/probs/out-of-credit', {
  detail: 'Your current balance is 30, but that costs 50.',
  instance: '/account/12345/msgs/abc',
  balance: 30,
  accounts: ['/account/12345', '/account/67890']
})

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


// HTTP defaults + Details
new Problem(403, {
  detail: 'Account suspended',
  instance: '/account/12345',
  date: '2016-01-15T06:47:01.175Z',
  account_id: '12345'
}) 
//=> { status: '403', title: 'Forbidden', type: 'http://www.iana.org/assignments/http-status-codes#404', detail: 'Account suspended', instance: '/account/12345', account_id: 12345, 'date: 2016-01-15T06:47:01.175Z' }

Method : toString()

returns a simplified, human-readable string representation

let prob = new Problem(403, 'You do not have enough credit', 'https://example.com/probs/out-of-credit') 

prob.toString() //=> [403] You do not have enough credit ('https://example.com/probs/out-of-credit')

Method : send(response)

uses response.writeHead and response.end to send an appropriate error respnse message with the Content-Type response header to application/problem+json

import http from 'http'
import Problem from 'api-problem'

let response = new http.ServerResponse()
Problem.send(response)

Express Middleware

A standard connect middleware is provided. The middleware intercepts Problem objects thrown as exceptions and serializes them appropriately in the HTTP response while setting the Content-Type response header to application/problem+json

import express from 'express'
import { Problem, Middleware } from 'api-problem'

let app = express()

app.get('/', (req, res) => {
  throw new Problem(403)
})

app.use(Problem.Middleware)

©️ www.ahmadnassri.com  ·  License: ISC  ·  Github: @ahmadnassri  ·  Twitter: @ahmadnassri