Skip to content

Commit 4510c13

Browse files
committed
Retry on node-xmlhttprequest connection error, and report useful error message
1 parent 9acf107 commit 4510c13

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/InstallationController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function hexOctet() {
1919
return Math.floor(
2020
(1 + Math.random()) * 0x10000
2121
).toString(16).substring(1);
22-
};
22+
}
2323

2424
function generateId() {
2525
return (

src/RESTController.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,15 @@ var RESTController = {
100100
promise.reject(e);
101101
}
102102
promise.resolve(response, xhr.status, xhr);
103-
} else if (xhr.status >= 500) { // retry on 5XX
103+
} else if (xhr.status >= 500 || xhr.status === 0) { // retry on 5XX or node-xmlhttprequest error
104104
if (++attempts < CoreManager.get('REQUEST_ATTEMPT_LIMIT')) {
105105
// Exponentially-growing random delay
106106
var delay = Math.round(
107107
Math.random() * 125 * Math.pow(2, attempts)
108108
);
109109
setTimeout(dispatch, delay);
110+
} else if (xhr.status === 0) {
111+
promise.reject('Unable to connect to the Parse API');
110112
} else {
111113
// After the retry limit is reached, fail
112114
promise.reject(xhr);

src/__tests__/RESTController-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ describe('RESTController', () => {
7171
jest.runAllTimers();
7272
}));
7373

74+
it('retries on connection failure', asyncHelper((done) => {
75+
RESTController._setXHR(mockXHR([
76+
{ status: 0 },
77+
{ status: 0 },
78+
{ status: 0 },
79+
{ status: 0 },
80+
{ status: 0 },
81+
]));
82+
RESTController.ajax('POST', 'users', {}).then(null, (err) => {
83+
expect(err).toBe('Unable to connect to the Parse API');
84+
done();
85+
});
86+
jest.runAllTimers();
87+
}));
88+
89+
it('returns a connection error on network failure', asyncHelper((done) => {
90+
RESTController._setXHR(mockXHR([
91+
{ status: 0 },
92+
{ status: 0 },
93+
{ status: 0 },
94+
{ status: 0 },
95+
{ status: 0 },
96+
]));
97+
RESTController.request('GET', 'classes/MyObject', {}, { sessionToken: '1234' }).then(null, (err) => {
98+
expect(err.code).toBe(100);
99+
expect(err.message).toBe('XMLHttpRequest failed: "Unable to connect to the Parse API"');
100+
done();
101+
});
102+
jest.runAllTimers();
103+
}));
104+
74105
it('aborts after too many failures', asyncHelper((done) => {
75106
RESTController._setXHR(mockXHR([
76107
{ status: 500 },

0 commit comments

Comments
 (0)