|
@@ -1,15 +1,25 @@
|
|
1
|
+import { timeoutPromise } from './timeoutPromise';
|
|
2
|
+
|
1
|
3
|
const logger = require('jitsi-meet-logger').getLogger(__filename);
|
2
|
4
|
|
|
5
|
+/**
|
|
6
|
+ * The number of milliseconds before deciding that we need retry a fetch request.
|
|
7
|
+ *
|
|
8
|
+ * @type {number}
|
|
9
|
+ */
|
|
10
|
+const RETRY_TIMEOUT = 3000;
|
|
11
|
+
|
3
|
12
|
/**
|
4
|
13
|
* Wrapper around fetch GET requests to handle json-ifying the response
|
5
|
14
|
* and logging errors.
|
6
|
15
|
*
|
7
|
16
|
* @param {string} url - The URL to perform a GET against.
|
|
17
|
+ * @param {?boolean} retry - Whether the request will be retried after short timeout.
|
8
|
18
|
* @returns {Promise<Object>} The response body, in JSON format, will be
|
9
|
19
|
* through the Promise.
|
10
|
20
|
*/
|
11
|
|
-export function doGetJSON(url) {
|
12
|
|
- return fetch(url)
|
|
21
|
+export function doGetJSON(url, retry) {
|
|
22
|
+ const fetchPromise = fetch(url)
|
13
|
23
|
.then(response => {
|
14
|
24
|
const jsonify = response.json();
|
15
|
25
|
|
|
@@ -25,4 +35,17 @@ export function doGetJSON(url) {
|
25
|
35
|
|
26
|
36
|
return Promise.reject(error);
|
27
|
37
|
});
|
|
38
|
+
|
|
39
|
+ if (retry) {
|
|
40
|
+ return timeoutPromise(fetchPromise, RETRY_TIMEOUT)
|
|
41
|
+ .catch(response => {
|
|
42
|
+ if (response.status >= 400 && response.status < 500) {
|
|
43
|
+ return Promise.reject(response);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ return timeoutPromise(fetchPromise, RETRY_TIMEOUT);
|
|
47
|
+ });
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ return fetchPromise;
|
28
|
51
|
}
|