Skip to content

Commit 89b1d8b

Browse files
authored
fix all lifecycle hooks (#2480)
1 parent 97015ba commit 89b1d8b

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

docs/config/config-file.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ import { defineConfig } from "@trigger.dev/sdk";
9999
export default defineConfig({
100100
project: "<project ref>",
101101
// Your other config settings...
102-
onSuccess: async (payload, output, { ctx }) => {
102+
onSuccess: async ({ payload, output, ctx }) => {
103103
console.log("Task succeeded", ctx.task.id);
104104
},
105-
onFailure: async (payload, error, { ctx }) => {
105+
onFailure: async ({ payload, error, ctx }) => {
106106
console.log("Task failed", ctx.task.id);
107107
},
108-
onStart: async (payload, { ctx }) => {
108+
onStart: async ({ payload, ctx }) => {
109109
console.log("Task started", ctx.task.id);
110110
},
111-
init: async (payload, { ctx }) => {
111+
init: async ({ payload, ctx }) => {
112112
console.log("I run before any task is run");
113113
},
114114
});

docs/errors-retrying.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export const taskWithFetchRetries = task({
180180

181181
## Advanced error handling and retrying
182182

183-
We provide a `handleError` callback on the task and in your `trigger.config` file. This gets called when an uncaught error is thrown in your task.
183+
We provide a `catchError` callback on the task and in your `trigger.config` file. This gets called when an uncaught error is thrown in your task.
184184

185185
You can
186186

@@ -219,7 +219,7 @@ export const openaiTask = task({
219219

220220
return chatCompletion.choices[0].message.content;
221221
},
222-
handleError: async (payload, error, { ctx, retryAt }) => {
222+
catchError: async ({ payload, error, ctx, retryAt }) => {
223223
if (error instanceof OpenAI.APIError) {
224224
if (!error.status) {
225225
return {

docs/migrating-from-v3.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ import { task } from "@trigger.dev/sdk";
232232

233233
export const myTask = task({
234234
id: "my-task",
235-
onStart: (payload, { ctx }) => {},
235+
onStart: ({ payload, ctx }) => {},
236236
run: async (payload, { ctx }) => {},
237237
});
238238
```

docs/tasks/overview.mdx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ This function is called before a run attempt:
181181
```ts /trigger/init.ts
182182
export const taskWithInit = task({
183183
id: "task-with-init",
184-
init: async (payload, { ctx }) => {
184+
init: async ({ payload, ctx }) => {
185185
//...
186186
},
187187
run: async (payload: any, { ctx }) => {
@@ -195,7 +195,7 @@ You can also return data from the `init` function that will be available in the
195195
```ts /trigger/init-return.ts
196196
export const taskWithInitReturn = task({
197197
id: "task-with-init-return",
198-
init: async (payload, { ctx }) => {
198+
init: async ({ payload, ctx }) => {
199199
return { someData: "someValue" };
200200
},
201201
run: async (payload: any, { ctx, init }) => {
@@ -213,7 +213,7 @@ This function is called after the `run` function is executed, regardless of whet
213213
```ts /trigger/cleanup.ts
214214
export const taskWithCleanup = task({
215215
id: "task-with-cleanup",
216-
cleanup: async (payload, { ctx }) => {
216+
cleanup: async ({ payload, ctx }) => {
217217
//...
218218
},
219219
run: async (payload: any, { ctx }) => {
@@ -230,7 +230,7 @@ Our task middleware system runs at the top level, executing before and after all
230230

231231
<Info>
232232
An error thrown in `middleware` is just like an uncaught error in the run function: it will
233-
propagate through to `handleError()` and then will fail the attempt (causing a retry).
233+
propagate through to `catchError()` function and then will fail the attempt (causing a retry).
234234
</Info>
235235

236236
The `locals` API allows you to share data between middleware and hooks.
@@ -303,7 +303,7 @@ When a task run starts, the `onStart` function is called. It's useful for sendin
303303
```ts /trigger/on-start.ts
304304
export const taskWithOnStart = task({
305305
id: "task-with-on-start",
306-
onStart: async (payload, { ctx }) => {
306+
onStart: async ({ payload, ctx }) => {
307307
//...
308308
},
309309
run: async (payload: any, { ctx }) => {
@@ -319,7 +319,7 @@ import { defineConfig } from "@trigger.dev/sdk";
319319

320320
export default defineConfig({
321321
project: "proj_1234",
322-
onStart: async (payload, { ctx }) => {
322+
onStart: async ({ payload, ctx }) => {
323323
console.log("Task started", ctx.task.id);
324324
},
325325
});
@@ -357,7 +357,7 @@ When a task run succeeds, the `onSuccess` function is called. It's useful for se
357357
```ts /trigger/on-success.ts
358358
export const taskWithOnSuccess = task({
359359
id: "task-with-on-success",
360-
onSuccess: async (payload, output, { ctx }) => {
360+
onSuccess: async ({ payload, output, ctx }) => {
361361
//...
362362
},
363363
run: async (payload: any, { ctx }) => {
@@ -373,7 +373,7 @@ import { defineConfig } from "@trigger.dev/sdk";
373373

374374
export default defineConfig({
375375
project: "proj_1234",
376-
onSuccess: async (payload, output, { ctx }) => {
376+
onSuccess: async ({ payload, output, ctx }) => {
377377
console.log("Task succeeded", ctx.task.id);
378378
},
379379
});
@@ -388,7 +388,7 @@ This hook is executed when a run completes, regardless of whether it succeeded o
388388
```ts /trigger/on-complete.ts
389389
export const taskWithOnComplete = task({
390390
id: "task-with-on-complete",
391-
onComplete: async (payload, output, { ctx }) => {
391+
onComplete: async ({ payload, output, ctx }) => {
392392
if (result.ok) {
393393
console.log("Run succeeded", result.data);
394394
} else {
@@ -404,7 +404,7 @@ When a task run fails, the `onFailure` function is called. It's useful for sendi
404404
```ts /trigger/on-failure.ts
405405
export const taskWithOnFailure = task({
406406
id: "task-with-on-failure",
407-
onFailure: async (payload, error, { ctx }) => {
407+
onFailure: async ({ payload, error, ctx }) => {
408408
//...
409409
},
410410
run: async (payload: any, { ctx }) => {
@@ -420,7 +420,7 @@ import { defineConfig } from "@trigger.dev/sdk";
420420

421421
export default defineConfig({
422422
project: "proj_1234",
423-
onFailure: async (payload, error, { ctx }) => {
423+
onFailure: async ({ payload, error, ctx }) => {
424424
console.log("Task failed", ctx.task.id);
425425
},
426426
});
@@ -429,14 +429,15 @@ export default defineConfig({
429429
<Info>Errors thrown in the `onFailure` function are ignored.</Info>
430430
431431
<Note>
432-
`onFailure` doesn’t fire for some of the run statuses like `Crashed`, `System failures`, and `Canceled`.
432+
`onFailure` doesn’t fire for some of the run statuses like `Crashed`, `System failures`, and
433+
`Canceled`.
433434
</Note>
434435
435-
### `handleError` functions
436+
### `catchError` functions
436437
437438
You can define a function that will be called when an error is thrown in the `run` function, that allows you to control how the error is handled and whether the task should be retried.
438439
439-
Read more about `handleError` in our [Errors and Retrying guide](/errors-retrying).
440+
Read more about `catchError` in our [Errors and Retrying guide](/errors-retrying).
440441
441442
<Info>Uncaught errors will throw a special internal error of the type `HANDLE_ERROR_ERROR`.</Info>
442443

0 commit comments

Comments
 (0)