Skip to content

Commit

Permalink
Catch json parsing errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
omegascorp committed Jun 9, 2017
1 parent 693efae commit 7d0489a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/owl-amd.min.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion dist/owl-commonjs-ajax.js
Expand Up @@ -47,7 +47,12 @@ var owl = {
error;
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
response = JSON.parse(xhr.responseText);
try {
response = JSON.parse(xhr.responseText);
} catch (err) {
reject(err);
return;
}
settings.success && settings.success(response);
resolve(response);
} else {
Expand Down
7 changes: 6 additions & 1 deletion dist/owl-commonjs-core.js
Expand Up @@ -968,7 +968,12 @@ var owl = {
error;
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
response = JSON.parse(xhr.responseText);
try {
response = JSON.parse(xhr.responseText);
} catch (err) {
reject(err);
return;
}
settings.success && settings.success(response);
resolve(response);
} else {
Expand Down
7 changes: 6 additions & 1 deletion dist/owl-commonjs.js
Expand Up @@ -968,7 +968,12 @@ var owl = {
error;
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
response = JSON.parse(xhr.responseText);
try {
response = JSON.parse(xhr.responseText);
} catch (err) {
reject(err);
return;
}
settings.success && settings.success(response);
resolve(response);
} else {
Expand Down
2 changes: 1 addition & 1 deletion dist/owl.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "owljs",
"version": "0.7.4",
"version": "0.7.5",
"description": "Backbone-like frontend library",
"main": "index.js",
"directories": {
Expand Down
7 changes: 6 additions & 1 deletion src/owl.ajax.js
Expand Up @@ -36,7 +36,12 @@
error;
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
response = JSON.parse(xhr.responseText);
try {
response = JSON.parse(xhr.responseText);
} catch (err) {
reject(err);
return;
}
settings.success && settings.success(response);
resolve(response);
} else {
Expand Down
19 changes: 19 additions & 0 deletions test/owl.ajax.js
Expand Up @@ -21,6 +21,11 @@ describe('owl.ajax', function() {
{ 'Content-Type': 'application/json' },
'{}'
]);
server.respondWith('GET', '/something-wrong', [
200,
{ 'Content-Type': 'application/json' },
'{somethingInvalid:'
]);

describe('request', function() {
it('should make GET by default', function(done) {
Expand Down Expand Up @@ -111,6 +116,20 @@ describe('owl.ajax', function() {
});
});

describe('request (invalid)', function() {
it('should catch error', function(done) {
owl.ajax.request({
url: '/something-wrong',
type: 'GET'
}).catch(function(error) {
done();
owl.ajax.restore();
});

server.respond();
});
});

describe('toJsonString', function() {
it('stringify object', function() {
expect(owl.ajax.toJsonString({
Expand Down

0 comments on commit 7d0489a

Please sign in to comment.