Skip to content

Fix #1955: schemaTask parse payload failures handled gracefully #2043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/core/src/v3/workers/taskExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,21 @@ export class TaskExecutor {
);
}

parsedPayload = await this.#parsePayload(payloadResult);
const [parsePayloadError, parsedPayloadResult] = await tryCatch(
this.#parsePayload(payloadResult)
);

if (parsePayloadError) {
recordSpanException(span, parsePayloadError);
return this.#internalErrorResult(
execution,
TaskRunErrorCodes.TASK_INPUT_ERROR,
parsePayloadError,
true
);
}

parsedPayload = parsedPayloadResult;

lifecycleHooks.registerOnWaitHookListener(async (wait) => {
await this.#callOnWaitFunctions(wait, parsedPayload, ctx, initOutput, signal);
Expand Down Expand Up @@ -1369,7 +1383,12 @@ export class TaskExecutor {
});
}

#internalErrorResult(execution: TaskRunExecution, code: TaskRunErrorCodes, error: unknown) {
#internalErrorResult(
execution: TaskRunExecution,
code: TaskRunErrorCodes,
error: unknown,
skippedRetrying?: boolean
) {
return {
ok: false,
id: execution.run.id,
Expand All @@ -1384,6 +1403,7 @@ export class TaskExecutor {
: undefined,
stackTrace: error instanceof Error ? error.stack : undefined,
},
skippedRetrying,
} satisfies TaskRunExecutionResult;
}

Expand Down
37 changes: 36 additions & 1 deletion packages/core/test/taskExecutor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,37 @@ describe("TaskExecutor", () => {
expect(delay).toBeGreaterThan(29900); // Allow for some time passing during test
expect(delay).toBeLessThan(32000); // Account for max 2000ms jitter
});

test("should return error and skip retrying if parsePayload throws", async () => {
const parseError = new Error("Parse failed");
const task = {
id: "test-task",
fns: {
run: async () => {
throw new Error("Should not reach run");
},
parsePayload: async () => {
throw parseError;
},
},
};

const result = await executeTask(task, { foo: "bar" }, undefined);

expect(result).toEqual({
result: {
ok: false,
id: "test-run-id",
error: {
type: "INTERNAL_ERROR",
code: TaskRunErrorCodes.TASK_INPUT_ERROR,
message: "TaskPayloadParsedError: Parsing payload with schema failed: Parse failed",
stackTrace: expect.any(String),
},
skippedRetrying: true,
},
});
});
});

function executeTask(
Expand All @@ -1828,7 +1859,11 @@ function executeTask(
logger: tracingSDK.getLogger("test-task"),
});

const consoleInterceptor = new ConsoleInterceptor(tracingSDK.getLogger("test-task"), false);
const consoleInterceptor = new ConsoleInterceptor(
tracingSDK.getLogger("test-task"),
false,
false
);

const executor = new TaskExecutor(task, {
tracingSDK,
Expand Down