-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathretry.js
39 lines (36 loc) · 991 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
// yarn add p-retry
//
// Run this file as:
//
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node retry.js
//
// You may need to build the project first using:
//
// yarn prepack
//
const pRetry = require('p-retry')
const { Transloadit, ApiError } = require('transloadit')
const transloadit = new Transloadit({
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
})
async function run() {
console.log('Trying...')
try {
const { items } = await transloadit.listTemplates({ sort: 'created', order: 'asc' })
return items
} catch (err) {
if (err instanceof ApiError && err.code === 'INVALID_SIGNATURE') {
// This is an unrecoverable error, abort retry
throw new pRetry.AbortError('INVALID_SIGNATURE')
}
throw err
}
}
;(async () => {
try {
console.log(await pRetry(run, { retries: 5 }))
} catch (err) {
console.error('Operation failed', err)
}
})()