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

Trap errors from the export endpoint #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ dist/*
sandbox.js
index.html
npm-debug.log
.DS_Store
19 changes: 11 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,11 @@ 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) {
if (status && status < 200 || status > 299) {
return { error: result }
}

if (parameters && parameters.format === "csv") {
return result;
}
Expand All @@ -168,10 +171,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