handle-xhr-error
v2.0.0
Published
emit error when encountering an xhr error
Downloads
1
Readme
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
orlogin
- cb: optional callback to pass the error obj back to