handle-xhr-error

2.0.0 • Public • Published

handle-xhr-error 🚨

A tiny module to handle errors in an xhr request in an emitter based application.

var handleXHRError = require('handle-xhr-error')
var emitter = require('nanobus')()
var xhr = require('xhr')
 
var opts = {
  method: 'PUT',
  url: 'user/2',
  json: { kitty: cat },
  headers: {
    'content-type': 'application/json'
  }
}
 
xhr(opts, function (err, res, data) {
    if (err || res.statusCode > 299) {
      return handleXHRError(data, err, res.statusCode, emitter)
    }
    emitter.emit('user:message', 'Your password has been updated')
})

API

handleXHRError(data, error, emitter [,cb])

  • data: data to pass on to the error message
  • error.err: error to emit
  • error.statusCode: error status code
  • emitter.emitter: an instance of an emitter you're using
  • emitter.namespace: namespace used for the emitter, e.g. user or login
  • cb: optional callback to pass the error obj back to

Related Content:

  • xhr -- a small wrapper for xhr requests
  • nanobus -- a tiny message bus

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i handle-xhr-error

Weekly Downloads

1

Version

2.0.0

License

MIT

Last publish

Collaborators

  • lrlna