db-vendo-client/retry.js

40 lines
902 B
JavaScript
Raw Normal View History

2019-02-08 13:13:53 +01:00
'use strict'
const retry = require('p-retry')
const defaultProfile = require('./lib/default-profile')
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-10-31 18:48:11 +01:00
const withRetrying = (profile, retryOpts = {}) => {
2019-02-08 14:16:43 +01:00
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
})
2019-02-08 13:13:53 +01:00
}
2019-10-31 18:48:11 +01:00
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
}
module.exports = withRetrying