(
fn: (options: { attempt: number; maxAttempts: number }) => Promise<T>,
options: RetryOptions
)
| 29 | export type { RetryOptions }; |
| 30 | |
| 31 | function onThrow<T>( |
| 32 | fn: (options: { attempt: number; maxAttempts: number }) => Promise<T>, |
| 33 | options: RetryOptions |
| 34 | ): Promise<T> { |
| 35 | const opts = { |
| 36 | ...defaultRetryOptions, |
| 37 | ...options, |
| 38 | }; |
| 39 | |
| 40 | return tracer.startActiveSpan( |
| 41 | `retry.onThrow()`, |
| 42 | async (span) => { |
| 43 | let attempt = 1; |
| 44 | |
| 45 | while (attempt <= opts.maxAttempts) { |
| 46 | const innerSpan = tracer.startSpan("retry.fn()", { |
| 47 | attributes: { |
| 48 | [SemanticInternalAttributes.STYLE_ICON]: "function", |
| 49 | ...accessoryAttributes({ |
| 50 | items: [ |
| 51 | { |
| 52 | text: `${attempt}/${opts.maxAttempts}`, |
| 53 | variant: "normal", |
| 54 | }, |
| 55 | ], |
| 56 | style: "codepath", |
| 57 | }), |
| 58 | }, |
| 59 | }); |
| 60 | |
| 61 | const contextWithSpanSet = trace.setSpan(context.active(), innerSpan); |
| 62 | |
| 63 | try { |
| 64 | const result = await context.with(contextWithSpanSet, async () => { |
| 65 | return fn({ attempt, maxAttempts: opts.maxAttempts }); |
| 66 | }); |
| 67 | |
| 68 | innerSpan.end(); |
| 69 | |
| 70 | return result; |
| 71 | } catch (e) { |
| 72 | if (e instanceof Error || typeof e === "string") { |
| 73 | innerSpan.recordException(e); |
| 74 | } else { |
| 75 | innerSpan.recordException(String(e)); |
| 76 | } |
| 77 | |
| 78 | innerSpan.setStatus({ code: SpanStatusCode.ERROR }); |
| 79 | |
| 80 | const nextRetryDelay = calculateNextRetryDelay(opts, attempt); |
| 81 | |
| 82 | if (!nextRetryDelay) { |
| 83 | innerSpan.end(); |
| 84 | |
| 85 | throw e; |
| 86 | } |
| 87 | |
| 88 | innerSpan.setAttribute( |
nothing calls this directly
no test coverage detected
searching dependent graphs…