2.0.0 • Published 7 years ago

handle-xhr-error v2.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

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