mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-22 22:59:35 +02:00
optional retry support
This commit is contained in:
parent
3f58d84de5
commit
b0f786c42a
2 changed files with 35 additions and 0 deletions
|
@ -6,6 +6,7 @@
|
|||
"files": [
|
||||
"index.js",
|
||||
"throttle.js",
|
||||
"retry.js",
|
||||
"lib",
|
||||
"parse",
|
||||
"format",
|
||||
|
|
34
retry.js
Normal file
34
retry.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
'use strict'
|
||||
|
||||
const retry = require('p-retry')
|
||||
|
||||
const request = require('./lib/request')
|
||||
const createClient = require('.')
|
||||
|
||||
const defaultRetryOpts = {
|
||||
retries: 3,
|
||||
factor: 3,
|
||||
minTimeout: 5 * 1000
|
||||
}
|
||||
|
||||
const createClientWithRetry = (profile, userAgent, retryOpts = defaultRetryOpts) => {
|
||||
const requestWithRetry = (profile, userAgent, opt, data) => {
|
||||
const attempt = () => {
|
||||
return request(profile, userAgent, opt, data)
|
||||
.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 createClient(profile, userAgent, requestWithRetry)
|
||||
}
|
||||
|
||||
module.exports = createClientWithRetry
|
Loading…
Add table
Reference in a new issue