Skip to content

Add a failure protocol for Server Actions - #8

Merged
javaquasar merged 2 commits into
mainfrom
claude/action-failures
Dec 2, 2026
Merged

javaquasar merged 2 commits into
mainfrom
claude/action-failures

Conversation

@javaquasar

@javaquasar javaquasar commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

In production, Next.js replaces the message of an error thrown by a Server Action with a generic one. Field errors and business messages such as "this title already exists" never reached the client unless the app used next-safe-action. This PR adds a failure protocol that survives production builds.

"use server";
export const createTodoAction = withFlowContract(createTodoContract, async (input: CreateTodoInput) => {
  if (await titleExists(input.title)) {
    return actionFailure<CreateTodoInput>({ code: "conflict", fieldErrors: { title: ["Already exists"] } });
  }
  return insertTodo(input.title);
}, { revalidateTag, revalidatePath });

Server

  • actionFailure({ code, message, fieldErrors, formErrors }) returns a plain serializable object. The keys of fieldErrors are checked against the input type.
  • throw new FlowActionError(...) works from deeper code. withFlowContract, or withActionFailures when there is no contract, returns it as a failure. Other errors are rethrown unchanged.
  • withFlowContract invalidates nothing for a failure, because the mutation did not happen.

Client

  • runFlow turns a returned failure into a FlowActionError with the same code, fieldErrors and formErrors. This works for any action or adapter, and runs inside the middleware chain. The run settles as an error and rolls back optimistic state. It is not retried, because the same input would fail the same way.
  • Types. FlowConfig.action may resolve to ActionFailure, and the flow's TResult excludes it. result stays Todo in reconcile, redirect and data, for both adapters and plain functions.
  • useFlowForm maps the failure to fieldErrors / formErrors. A failure that has only a message becomes a form error.
  • Sentry sink. Expected failures still get a breadcrumb but no longer create issues. captureError: true captures them again.

Behaviour changes (CHANGELOG → Changed)

  • withFlowContract now returns TResult | ActionFailure<TInput>.
  • The Sentry sink default changed as described above.
  • resolveFlowAction and FlowConfig.action allow resolving to an ActionFailure.

Test plan

  • npm run typecheck from a clean build. New tests/types/failures.ts checks that:
    • the failure is excluded from TResult for adapters and plain actions;
    • fieldErrors keys are typed from the input;
    • the result type has not silently become any;
    • the withFlowContract and withActionFailures return types are correct.
  • npm test: 61/61. New tests cover:
    • converting a failure to a FlowActionError and back, and JSON serialization;
    • runFlow rolls back, does not retry, and emits the expected events;
    • withFlowContract skips invalidation for returned and thrown failures and rethrows unexpected errors;
    • useFlowForm shows field and form errors;
    • the Sentry sink default and captureError: true.
  • examples/next-app: typecheck and next build.
  • Production run (next build && next start) in a browser:
    • submitting a duplicate title shows the server field error "Read Mutaflow source" already exists;
    • the typed value is kept and the optimistic item rolls back;
    • the server log shows invalidation only for the successful create;
    • no console errors.

🤖 Generated with Claude Code

In production Next.js replaces the message of an error thrown by a
Server Action, so field errors and business messages never reached the
client unless next-safe-action was used.

A Server Action now returns actionFailure({ code, message, fieldErrors,
formErrors }), a serializable object with field names typed from the
input, or throws FlowActionError inside withActionFailures or
withFlowContract, which return it as a failure. withFlowContract skips
invalidation for failures.

On the client, runFlow turns a returned failure into a FlowActionError
for any action or adapter: the run settles as an error, rolls back and
is not retried. ActionFailure is excluded from the flow result type.
useFlowForm maps the failure to fieldErrors / formErrors, and the Sentry
sink keeps a breadcrumb but no longer creates issues for expected
failures unless captureError is true.

The next-app example returns a field error for a duplicate title,
checked in a production build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@javaquasar
javaquasar changed the base branch from claude/flow-forms to main December 2, 2026 17:46
…aude/action-failures

# Conflicts:
#	CHANGELOG.md
#	packages/mutaflow/src/types.ts
@javaquasar
javaquasar merged commit 53a04ee into main Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant