db-vendo-client/retry.js

42 lines
922 B
JavaScript
Raw Permalink Normal View History

import retry from 'p-retry';
import {defaultProfile} from './lib/default-profile.js';
2019-02-08 13:13:53 +01:00
2019-02-08 14:16:43 +01:00
const retryDefaults = {
2019-02-08 13:13:53 +01:00
retries: 3,
factor: 3,
minTimeout: 5 * 1000,
};
2019-02-08 13:13:53 +01:00
2019-10-31 18:48:11 +01:00
const withRetrying = (profile, retryOpts = {}) => {
retryOpts = Object.assign({}, retryDefaults, retryOpts);
// https://github.com/public-transport/hafas-client/issues/76#issuecomment-574408717
const {request} = {...defaultProfile, ...profile};
2019-10-31 18:48:11 +01:00
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);
};
2019-02-08 13:13:53 +01:00
2019-10-31 18:48:11 +01:00
return {
...profile,
request: retryingRequest,
};
};
2019-02-08 13:13:53 +01:00
2022-05-07 16:17:37 +02:00
export {
withRetrying,
};