Skip to content

Commit e90a58b

Browse files
feat: allow to pass errorData in pTimeout/pRetry
1 parent b4afcd7 commit e90a58b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/promise/pRetry.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { _since, _stringifyAny, AnyFunction, CommonLogger } from '..'
1+
import { _since, _stringifyAny, AnyFunction, AnyObject, AppError, CommonLogger } from '..'
22
import { TimeoutError } from './pTimeout'
33

44
export interface PRetryOptions {
@@ -91,6 +91,11 @@ export interface PRetryOptions {
9191
* @experimental
9292
*/
9393
keepStackTrace?: boolean
94+
95+
/**
96+
* Will be merged with `err.data` object.
97+
*/
98+
errorData?: AnyObject
9499
}
95100

96101
/**
@@ -139,7 +144,7 @@ export async function pRetry<T>(
139144
return await new Promise((resolve, reject) => {
140145
const rejectWithTimeout = () => {
141146
timedOut = true // to prevent more tries
142-
const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms`)
147+
const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms`, opt.errorData)
143148
if (fakeError) {
144149
// keep original stack
145150
err.stack = fakeError.stack!.replace('Error: RetryError', 'TimeoutError')
@@ -199,6 +204,11 @@ export async function pRetry<T>(
199204
})
200205
}
201206

207+
;(err as AppError).data = {
208+
...(err as AppError).data,
209+
...opt.errorData,
210+
}
211+
202212
reject(err)
203213
} else {
204214
// Retry after delay

src/promise/pTimeout.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AppError } from '../error/app.error'
2-
import { AnyFunction } from '../types'
2+
import { AnyFunction, AnyObject } from '../types'
33

44
export class TimeoutError extends AppError {}
55

@@ -29,6 +29,11 @@ export interface PTimeoutOptions {
2929
* @experimental
3030
*/
3131
keepStackTrace?: boolean
32+
33+
/**
34+
* Will be merged with `err.data` object.
35+
*/
36+
errorData?: AnyObject
3237
}
3338

3439
/**
@@ -63,12 +68,19 @@ export async function pTimeout<T>(promise: Promise<T>, opt: PTimeoutOptions): Pr
6368
resolve(onTimeout())
6469
} catch (err: any) {
6570
if (fakeError) err.stack = fakeError.stack // keep original stack
71+
err.data = {
72+
...err.data,
73+
...opt.errorData,
74+
}
6675
reject(err)
6776
}
6877
return
6978
}
7079

71-
const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`)
80+
const err = new TimeoutError(
81+
`"${name || 'pTimeout function'}" timed out after ${timeout} ms`,
82+
opt.errorData,
83+
)
7284
if (fakeError) err.stack = fakeError.stack // keep original stack
7385
reject(err)
7486
}, timeout)

0 commit comments

Comments
 (0)