MCPcopy
hub / github.com/triggerdotdev/trigger.dev / onThrow

Function onThrow

packages/trigger-sdk/src/v3/retry.ts:31–110  ·  view source on GitHub ↗
(
  fn: (options: { attempt: number; maxAttempts: number }) => Promise<T>,
  options: RetryOptions
)

Source from the content-addressed store, hash-verified

29export type { RetryOptions };
30
31function 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(

Callers

nothing calls this directly

Calls 5

accessoryAttributesFunction · 0.90
calculateNextRetryDelayFunction · 0.90
startActiveSpanMethod · 0.80
startSpanMethod · 0.80
waitForDurationMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…