Need private packages and team management tools?Check out npm Teams »

is-fail

0.1.1 • Public • Published

is-fail Build Status NPM

Tiny and dependency-free node.js/io.js package implementing multiple strategies to verify if an HTTP request was failed, checking both error and HTTP response code.

Useful to combine as part of a retry/backoff logic. Also allows you to plug in additional strategies.

Installation

npm install is-fail --save

Usage

Simple usage as part of a retry mechanism

const http = require('http')
const isFail = require('is-fail')()
 
function doRequest() {
  http.request('http://inconsistent-server', function (res) {
    if (isFail(null, res)) {
      return doRequest() // retry!
    }
    console.error('Response received')
  })
  .on('error', function (err) {
    if (isFail(err)) {
      return doRequest() // retry!
    }
    console.error('The request failed')
  })
}
 
doRequest()

Plug in additional strategies

const http = require('http')
const isFail = require('is-fail')()
 
function notFoundStrategy(err, res) {
  return res.statusCode === 404
}
 
const checkFail = isFail([ notFoundStrategy ])
 
http.request('http://inconsistent-server', function (res) {
  if (checkFail(null, res)) {
    console.log('Failed request!')
  }
})

API

isFail([ strategies ]) => Function(err, res) => Boolean

isFail.strategies = Array[ Strategy ]

isFail.check(strategies, err, res) => Boolean

isFail.errors = Array[ ErrorStrategy ]

isFail.status = Array[ StatusStrategy ]

License

MIT - Tomas Aparicio

Install

npm i is-fail

DownloadsWeekly Downloads

232

Version

0.1.1

License

MIT

Last publish

Collaborators

  • avatar