Repository navigation
Preserve ConvexError data across workflow steps and replay #316
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
Open
ianmacartney
wants to merge
3
commits into
main
Choose a base branch
from
issue-315
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| "@convex-dev/workflow": minor | ||
| --- | ||
|
|
||
| Preserve `ConvexError` identity and structured error data when catching failed | ||
| steps, including inline calls, queued functions, nested workflows, and journal | ||
| replay. Raises the `@convex-dev/workpool` peer minimum to 0.4.16. Oversized | ||
| failures are replaced with a bounded diagnostic so they can still be journaled. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { getStatus, WorkflowManager } from "@convex-dev/workflow"; | ||
| import { afterEach, beforeEach, expect, test, vi } from "vitest"; | ||
| import { components, internal } from "../_generated/api.js"; | ||
| import { initConvexTest } from "../setup.test.js"; | ||
| import { getConvexSize, type Value } from "convex/values"; | ||
|
|
||
| const workflow = new WorkflowManager(components.workflow); | ||
|
|
||
| beforeEach(() => vi.useFakeTimers()); | ||
| afterEach(() => vi.useRealTimers()); | ||
|
|
||
| test.each([ | ||
| { functionType: "query" as const, inline: true }, | ||
| { functionType: "mutation" as const, inline: true }, | ||
| { functionType: "query" as const, inline: false }, | ||
| { functionType: "mutation" as const, inline: false }, | ||
| { functionType: "action" as const, inline: false }, | ||
| ])( | ||
| "preserves ConvexError from $functionType (inline: $inline) through replay", | ||
| async (options) => { | ||
| const t = initConvexTest(); | ||
| const data = { | ||
| code: "INVALID_INPUT", | ||
| message: "Please try again", | ||
| count: 7, | ||
| }; | ||
| const workflowId = await t.mutation((ctx) => | ||
| workflow.start(ctx, internal.test.convexErrors.catchingWorkflow, { | ||
| ...options, | ||
| data, | ||
| }), | ||
| ); | ||
| await t.finishAllScheduledFunctions(vi.runAllTimers); | ||
| const status = await t.query((ctx) => | ||
| getStatus(ctx, components.workflow, workflowId), | ||
| ); | ||
| expect(status).toEqual({ | ||
| type: "completed", | ||
| result: { isConvexError: true, data }, | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| test("preserves an uncaught ConvexError from a nested workflow", async () => { | ||
| const t = initConvexTest(); | ||
| const data = { code: "CHILD_FAILED", message: "Please try again" }; | ||
| const workflowId = await t.mutation((ctx) => | ||
| workflow.start(ctx, internal.test.convexErrors.catchingNestedWorkflow, { | ||
| data, | ||
| }), | ||
| ); | ||
| await t.finishAllScheduledFunctions(vi.runAllTimers); | ||
| expect( | ||
| await t.query((ctx) => getStatus(ctx, components.workflow, workflowId)), | ||
| ).toEqual({ | ||
| type: "completed", | ||
| result: { isConvexError: true, data }, | ||
| }); | ||
| }); | ||
|
|
||
| test.each(["query", "mutation"] as const)( | ||
| "oversized inline %s failures remain catchable through replay", | ||
| async (functionType) => { | ||
| const t = initConvexTest(); | ||
| const workflowId = await t.mutation((ctx) => | ||
| workflow.start(ctx, internal.test.convexErrors.catchingOversizedError, { | ||
| functionType, | ||
| }), | ||
| ); | ||
| await t.finishAllScheduledFunctions(vi.runAllTimers); | ||
| const status = await t.query((ctx) => | ||
| getStatus(ctx, components.workflow, workflowId), | ||
| ); | ||
| expect(status.type).toBe("completed"); | ||
| if (status.type !== "completed") | ||
| throw new Error("Expected recovery to complete"); | ||
| expect(status.result).toMatchObject({ | ||
| isConvexError: false, | ||
| data: expect.stringContaining("Step failure too large"), | ||
| }); | ||
| expect(getConvexSize(status.result as Value)).toBeLessThan(100 << 10); | ||
| const steps = await t.query((ctx) => | ||
| workflow.listSteps(ctx, workflowId, { | ||
| paginationOpts: { numItems: 10, cursor: null }, | ||
| }), | ||
| ); | ||
| expect(steps.page).toHaveLength(2); | ||
| const failure = steps.page[0].runResult; | ||
| expect(failure).toMatchObject({ kind: "failed" }); | ||
| if (failure?.kind !== "failed") | ||
| throw new Error("Expected a recorded failure"); | ||
| expect(failure).not.toHaveProperty("errorData"); | ||
| expect(getConvexSize(failure)).toBeLessThan(100 << 10); | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import { WorkflowManager } from "@convex-dev/workflow"; | ||
| import { ConvexError, v, type Value } from "convex/values"; | ||
| import { components, internal } from "../_generated/api.js"; | ||
| import { | ||
| internalAction, | ||
| internalMutation, | ||
| internalQuery, | ||
| } from "../_generated/server.js"; | ||
|
|
||
| const workflow = new WorkflowManager(components.workflow); | ||
| const args = { data: v.any() }; | ||
| const fail = (_ctx: unknown, { data }: { data: Value }): never => { | ||
| throw new ConvexError(data); | ||
| }; | ||
|
|
||
| export const failingQuery = internalQuery({ | ||
| args, | ||
| returns: v.null(), | ||
| handler: fail, | ||
| }); | ||
| export const failingMutation = internalMutation({ | ||
| args, | ||
| returns: v.null(), | ||
| handler: fail, | ||
| }); | ||
| export const failingAction = internalAction({ | ||
| args, | ||
| returns: v.null(), | ||
| handler: fail, | ||
| }); | ||
|
|
||
| export const oversizedQuery = internalQuery({ | ||
| args: {}, | ||
| returns: v.null(), | ||
| handler: (ctx) => fail(ctx, { data: "x".repeat(900_000) }), | ||
| }); | ||
|
|
||
| export const oversizedMutation = internalMutation({ | ||
| args: {}, | ||
| returns: v.null(), | ||
| handler: (ctx) => fail(ctx, { data: "x".repeat(900_000) }), | ||
| }); | ||
|
|
||
| export const catchingOversizedError = workflow | ||
| .define({ | ||
| args: { functionType: v.union(v.literal("query"), v.literal("mutation")) }, | ||
| returns: v.object({ isConvexError: v.boolean(), data: v.any() }), | ||
| }) | ||
| .handler(async (step, { functionType }) => { | ||
| try { | ||
| if (functionType === "query") { | ||
| await step.runQuery( | ||
| internal.test.convexErrors.oversizedQuery, | ||
| {}, | ||
| { inline: true }, | ||
| ); | ||
| } else { | ||
| await step.runMutation( | ||
| internal.test.convexErrors.oversizedMutation, | ||
| {}, | ||
| { inline: true }, | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| // Journal these caught values in another step so replay must agree. | ||
| return await step.runAction(internal.test.convexErrors.echo, { | ||
| isConvexError: error instanceof ConvexError, | ||
| data: error instanceof Error ? error.message : String(error), | ||
| }); | ||
| } | ||
| throw new Error("Expected an oversized error"); | ||
| }); | ||
|
|
||
| export const echo = internalAction({ | ||
| args: { isConvexError: v.boolean(), data: v.any() }, | ||
| returns: v.object({ isConvexError: v.boolean(), data: v.any() }), | ||
| handler: async (_ctx, args) => args, | ||
| }); | ||
|
|
||
| export const failingWorkflow = workflow | ||
| .define({ args, returns: v.null() }) | ||
| .handler(async (_step, { data }) => { | ||
| throw new ConvexError(data); | ||
| }); | ||
|
|
||
| export const catchingNestedWorkflow = workflow | ||
| .define({ | ||
| args, | ||
| returns: v.object({ isConvexError: v.boolean(), data: v.any() }), | ||
| }) | ||
| .handler(async (step, { data }) => { | ||
| try { | ||
| await step.runWorkflow(internal.test.convexErrors.failingWorkflow, { | ||
| data, | ||
| }); | ||
| } catch (error) { | ||
| return await step.runAction(internal.test.convexErrors.echo, { | ||
| isConvexError: error instanceof ConvexError, | ||
| data: error instanceof ConvexError ? error.data : null, | ||
| }); | ||
| } | ||
| throw new Error("Expected the child workflow to fail"); | ||
| }); | ||
|
|
||
| export const catchingWorkflow = workflow | ||
| .define({ | ||
| args: { | ||
| data: v.any(), | ||
| functionType: v.union( | ||
| v.literal("query"), | ||
| v.literal("mutation"), | ||
| v.literal("action"), | ||
| ), | ||
| inline: v.boolean(), | ||
| }, | ||
| returns: v.object({ isConvexError: v.boolean(), data: v.any() }), | ||
| }) | ||
| .handler(async (step, { data, functionType, inline }) => { | ||
| let caught: { isConvexError: boolean; data: Value } = { | ||
| isConvexError: false, | ||
| data: null, | ||
| }; | ||
| try { | ||
| switch (functionType) { | ||
| case "query": | ||
| await step.runQuery( | ||
| internal.test.convexErrors.failingQuery, | ||
| { data }, | ||
| { inline }, | ||
| ); | ||
| break; | ||
| case "mutation": | ||
| await step.runMutation( | ||
| internal.test.convexErrors.failingMutation, | ||
| { data }, | ||
| { inline }, | ||
| ); | ||
| break; | ||
| case "action": | ||
| await step.runAction( | ||
| internal.test.convexErrors.failingAction, | ||
| { data }, | ||
| { retry: false }, | ||
| ); | ||
| break; | ||
| } | ||
| } catch (error) { | ||
| caught = { | ||
| isConvexError: error instanceof ConvexError, | ||
| data: error instanceof ConvexError ? error.data : null, | ||
| }; | ||
| } | ||
| // A queued recovery step forces replay of the failed step and validates | ||
| // that the caught data stays the same when used in subsequent step args. | ||
| return await step.runAction(internal.test.convexErrors.echo, caught); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.