Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bad json error handling, test error states #56

Merged
merged 2 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
56 changes: 56 additions & 0 deletions test/test-najax.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,62 @@ describe('defaults', function () {
})
})

describe('errors', function () {
describe('error response codes', function () {
[400, 422, 500, 502].forEach(function (code) {
describe('Status code: ' + code, function () {
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>'

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