-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
retry.js
41 lines (36 loc) · 922 Bytes
/
retry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import retry from 'p-retry';
import {defaultProfile} from './lib/default-profile.js';
const retryDefaults = {
retries: 3,
factor: 3,
minTimeout: 5 * 1000,
};
const withRetrying = (profile, retryOpts = {}) => {
retryOpts = Object.assign({}, retryDefaults, retryOpts);
// https://github.com/public-transport/hafas-client/issues/76#issuecomment-574408717
const {request} = {...defaultProfile, ...profile};
const retryingRequest = (...args) => {
const attempt = () => {
return request(...args)
.catch((err) => {
if (err.isHafasError) {
throw err;
} // continue
if (err.code === 'ENOTFOUND') { // abort
const abortErr = new retry.AbortError(err);
Object.assign(abortErr, err);
throw abortErr;
}
throw err; // continue
});
};
return retry(attempt, retryOpts);
};
return {
...profile,
request: retryingRequest,
};
};
export {
withRetrying,
};