Skip to content

Commit 15bc7c5

Browse files
author
Mike Carter
committed
Trap errors from the export endpoint
1 parent ed2332d commit 15bc7c5

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dist/*
55
sandbox.js
66
index.html
77
npm-debug.log
8+
.DS_Store

src/mixpanel_data_export.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var MixpanelExport = (function() {
8585
MixpanelExport.prototype.segmentation = function(parameters, callback) {
8686
return this.get(["segmentation"], parameters, callback);
8787
};
88-
88+
8989
MixpanelExport.prototype.multiseg = function(parameters, callback) {
9090
return this.get(["segmentation/multiseg"], parameters, callback);
9191
};
@@ -148,7 +148,7 @@ var MixpanelExport = (function() {
148148
request.open("get", this._buildRequestURL(method, parameters), true);
149149
request.setRequestHeader('Authorization', 'Basic ' + this._base64Encode(this.api_secret + ':'));
150150
request.onload = function() {
151-
callback(self._parseResponse(method, parameters, this.responseText));
151+
callback(self._parseResponse(method, parameters, this.responseText, this.status));
152152
};
153153
request.send();
154154
};
@@ -157,8 +157,13 @@ var MixpanelExport = (function() {
157157
return "MixpanelExport: The '" + methodName + "' method cannot be used in your browser.";
158158
};
159159

160-
// Parses Mixpanel's strange formatting for the export endpoint.
161-
MixpanelExport.prototype._parseResponse = function(method, parameters, result) {
160+
MixpanelExport.prototype._parseResponse = function(method, parameters, result, status) {
161+
// The export endpoint appears to give non-200 status codes on errors
162+
// which has changed at some point.
163+
if (status && status < 200 || status > 299) {
164+
return { error: result }
165+
}
166+
162167
if (parameters && parameters.format === "csv") {
163168
return result;
164169
}
@@ -168,10 +173,10 @@ var MixpanelExport = (function() {
168173
}
169174

170175
if (method === "export") {
171-
var step1 = result.replace(new RegExp('\n', 'g'), ',');
172-
var step2 = '['+step1+']';
173-
var result = step2.replace(',]', ']');
174-
return JSON.parse(result);
176+
// Parses Mixpanel's strange formatting for the export endpoint into (hopefully) JSON:
177+
var commaSeparated = result.replace(new RegExp('\n', 'g'), ',');
178+
var arrayed = '['+commaSeparated+']';
179+
result = arrayed.replace(',]', ']');
175180
}
176181

177182
return JSON.parse(result);

0 commit comments

Comments
 (0)