db-vendo-client/retry.js
Kristjan ESPERANTO 66d9fb5194
apply linting rules
follow-up of 228c7253
2024-02-10 16:50:12 +01:00

41 lines
922 B
JavaScript

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,
};