Skip to content

Commit

Permalink
Trap errors from the export endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Carter committed Nov 1, 2018
1 parent ed2332d commit 15bc7c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ dist/*
sandbox.js
index.html
npm-debug.log
.DS_Store
21 changes: 13 additions & 8 deletions src/mixpanel_data_export.js
Expand Up @@ -85,7 +85,7 @@ var MixpanelExport = (function() {
MixpanelExport.prototype.segmentation = function(parameters, callback) {
return this.get(["segmentation"], parameters, callback);
};

MixpanelExport.prototype.multiseg = function(parameters, callback) {
return this.get(["segmentation/multiseg"], parameters, callback);
};
Expand Down Expand Up @@ -148,7 +148,7 @@ var MixpanelExport = (function() {
request.open("get", this._buildRequestURL(method, parameters), true);
request.setRequestHeader('Authorization', 'Basic ' + this._base64Encode(this.api_secret + ':'));
request.onload = function() {
callback(self._parseResponse(method, parameters, this.responseText));
callback(self._parseResponse(method, parameters, this.responseText, this.status));
};
request.send();
};
Expand All @@ -157,8 +157,13 @@ var MixpanelExport = (function() {
return "MixpanelExport: The '" + methodName + "' method cannot be used in your browser.";
};

// Parses Mixpanel's strange formatting for the export endpoint.
MixpanelExport.prototype._parseResponse = function(method, parameters, result) {
MixpanelExport.prototype._parseResponse = function(method, parameters, result, status) {
// The export endpoint appears to give non-200 status codes on errors
// which has changed at some point.
if (status && status < 200 || status > 299) {
return { error: result }
}

if (parameters && parameters.format === "csv") {
return result;
}
Expand All @@ -168,10 +173,10 @@ var MixpanelExport = (function() {
}

if (method === "export") {
var step1 = result.replace(new RegExp('\n', 'g'), ',');
var step2 = '['+step1+']';
var result = step2.replace(',]', ']');
return JSON.parse(result);
// Parses Mixpanel's strange formatting for the export endpoint into (hopefully) JSON:
var commaSeparated = result.replace(new RegExp('\n', 'g'), ',');
var arrayed = '['+commaSeparated+']';
result = arrayed.replace(',]', ']');
}

return JSON.parse(result);
Expand Down

0 comments on commit 15bc7c5

Please sign in to comment.