Skip to content

Commit

Permalink
Fix 204 handling, update CHANGELOG, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinlarimer committed Aug 10, 2017
1 parent 928fad0 commit 415ba1a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

**FIXED:**
* Syntax errors caused by a malformed JSON response are now caught properly (#22).
* Successful delete responses (`204`s) are no longer handled as errors (#14).


<a name="1.2.2"></a>
Expand Down
34 changes: 24 additions & 10 deletions dist/keen-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ KeenLibrary.prototype.query = function(a, b){
.auth(this.readKey())
.send();
}
else if (a === 'dataset' && typeof b === 'object') {
return this
.get(this.url('datasets', b.name, 'results'))
.auth(this.readKey())
.send(b);
}
else if (a && b && typeof b === 'object') {
var q = extend({ analysis_type: a }, b);
return this
Expand Down Expand Up @@ -286,15 +292,22 @@ function sendXhr(method, config, callback){
var response;
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300) {
try {
response = JSON.parse( xhr.responseText );
if (cb && response) {
cb(null, response);
if (xhr.status === 204) {
if (cb) {
cb(null, xhr);
}
}
catch (e) {
if (cb) {
cb(xhr, null);
else {
try {
response = JSON.parse( xhr.responseText );
if (cb && response) {
cb(null, response);
}
}
catch (e) {
if (cb) {
cb(xhr, null);
}
}
}
}
Expand Down Expand Up @@ -3715,7 +3728,7 @@ process.umask = function() { return 0; };
debug: false,
enabled: true,
loaded: false,
version: '1.2.2'
version: '1.3.0'
});
Client.helpers = Client.helpers || {};
Client.resources = Client.resources || {};
Expand All @@ -3725,7 +3738,8 @@ process.umask = function() { return 0; };
'projects' : '{protocol}://{host}/3.0/projects',
'projectId' : '{protocol}://{host}/3.0/projects/{projectId}',
'events' : '{protocol}://{host}/3.0/projects/{projectId}/events',
'queries' : '{protocol}://{host}/3.0/projects/{projectId}/queries'
'queries' : '{protocol}://{host}/3.0/projects/{projectId}/queries',
'datasets' : '{protocol}://{host}/3.0/projects/{projectId}/datasets'
});
Client.utils = Client.utils || {};
extend(Client.utils, {
Expand Down Expand Up @@ -3779,7 +3793,7 @@ process.umask = function() { return 0; };
requestType : 'jsonp',
resources : extend({}, Client.resources)
};
if (typeof window !== 'undefined' && window.navigator.userAgent.indexOf('MSIE') > -1) {
if (typeof window !== 'undefined' && window.navigator && window.navigator.userAgent && window.navigator.userAgent.indexOf('MSIE') > -1) {
config.protocol = document.location.protocol.replace(':', '');
}
if (config.host) {
Expand Down
6 changes: 3 additions & 3 deletions dist/keen-analysis.min.js

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions lib/utils/http-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,22 @@ function sendXhr(method, config, callback){
var response;
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300) {
try {
response = JSON.parse( xhr.responseText );
if (cb && response) {
cb(null, response);
if (xhr.status === 204) {
if (cb) {
cb(null, xhr);
}
}
catch (e) {
if (cb) {
cb(xhr, null);
else {
try {
response = JSON.parse( xhr.responseText );
if (cb && response) {
cb(null, response);
}
}
catch (e) {
if (cb) {
cb(xhr, null);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ function handleRequest(config, callback){
body += d;
});
res.on('end', function() {
var parsedBody;
try {
var parsedBody = JSON.parse(body);
parsedBody = JSON.parse(body);
} catch (error) {
return callback(error, null);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/helpers/client-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
readKey : 'f979d53e026bdbf1ba16f01ce168bc7bcf9d5308ba672fb14e1834793a9e705eefa04793f0f87fb76b1d49a6cc2747b96a8abae0e4569d70314099b3f7790f55e98c9ac482e3883aab86a4bb577c295dbbca4867e95e2e4b15038fac5d80957ded3e868e4e6e319d3aa9275abc22b16e',
masterKey : 'FC135394DD08E3976870B7E7E83BDCD8',
protocol : 'https',
host : 'mocha.keen.io'
host : 'api.keen.io'
},
collection : 'keen-analysis',
properties: {
Expand Down

0 comments on commit 415ba1a

Please sign in to comment.