Skip to content

Commit

Permalink
Add error tests; Return parseerror like jquery when json is invalid.
Browse files Browse the repository at this point in the history
* Return original responseText in errors.  Stack trace is available in
error (3rd argument)
  • Loading branch information
demersus committed Mar 14, 2017
1 parent 3d9d048 commit 360ed15
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/najax.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,7 @@ function najax (uri, options, callback) {
jqXHR.responseText = data

var statusCode = res.statusCode
jqXHR.statusText = 'success'

if (statusCode === 204 || options.method === 'HEAD') {
jqXHR.statusText = 'nocontent'
} else if (statusCode === 304) {
jqXHR.statusText = 'notmodified'
}

//
// Determine if successful
// (per https://github.com/jquery/jquery/blob/master/src/ajax.js#L679)
var isSuccess = statusCode >= 200 && statusCode < 300 || statusCode === 304
Expand All @@ -156,11 +149,20 @@ function najax (uri, options, callback) {
try {
data = JSON.parse(data.replace(/[\cA-\cZ]/gi, ''))
} catch (e) {
jqXHR.statusText = 'parseerror'
return onError(e)
}
}

if (isSuccess) {
jqXHR.statusText = 'success'

if (statusCode === 204 || options.method === 'HEAD') {
jqXHR.statusText = 'nocontent'
} else if (statusCode === 304) {
jqXHR.statusText = 'notmodified'
}

// success, statusText, jqXHR
dfd.resolve(data, jqXHR.statusText, jqXHR)
} else {
Expand Down Expand Up @@ -202,8 +204,6 @@ function najax (uri, options, callback) {
req.on('error', onError)

function onError (e) {
// Set data for the fake xhr object
if (jqXHR.statusText === 'error') jqXHR.responseText = e.stack
// jqXHR, statusText, error
dfd.reject(jqXHR, jqXHR.statusText, e)
}
Expand Down
84 changes: 84 additions & 0 deletions test/test-najax.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,90 @@ describe('defaults', function () {
})
})

describe('errors', function () {
describe('error response codes', function () {
[400, 422, 500, 502].forEach(function (code) {
describe('Status code: ' + code, function () {
it('should trigger promise fail', function (done) {
nock('http://example.com').post('/').reply(code, 'An Error Occurred... Details here')

najax({url: 'http://example.com', type: 'POST'}).fail(function (jqXHR, statusText, error) {
expect(jqXHR.status).to.equal(code)
expect(jqXHR.responseText).to.equal('An Error Occurred... Details here')

expect(statusText).to.equal('error')
expect(error).to.be.an('error')

done()
})
})

describe('error callback given', function () {
it('should trigger callback', function (done) {
nock('http://example.com').post('/').reply(code, 'An Error Occurred... Details here')

najax({
url: 'http://example.com',
type: 'POST',
error: function (jqXHR, statusText, error) {
expect(jqXHR.status).to.equal(code)
expect(jqXHR.responseText).to.equal('An Error Occurred... Details here')

expect(statusText).to.equal('error')
expect(error).to.be.an('error')

done()
}
})
})
})
})
})
})

describe('error parsing json response', function () {
var options = {
url: 'http://example.com',
dataType: 'json',
method: 'POST'
}

var response = '<div>Not Json</div>'

it('should trigger promise fail', function (done) {
nock('http://example.com').post('/').reply(200, response)

najax(options).fail(function (jqXHR, statusText, error) {
expect(jqXHR.status).to.equal(200)
expect(jqXHR.responseText).to.equal(response)

expect(statusText).to.equal('parseerror')
expect(error).to.be.instanceOf(SyntaxError)

done()
})
})

describe('error callback given', function () {
it('should trigger callback', function (done) {
nock('http://example.com').post('/').reply(200, response)

najax(Object.assign({}, options, {
error: function (jqXHR, statusText, error) {
expect(jqXHR.status).to.equal(200)
expect(jqXHR.responseText).to.equal(response)

expect(statusText).to.equal('parseerror')
expect(error).to.be.instanceOf(SyntaxError)

done()
}
}))
})
})
})
})

function createSuccess (done) {
return function (data, statusText) {
expect(data).to.equal('ok')
Expand Down

0 comments on commit 360ed15

Please sign in to comment.