Repository navigation
Add a failure protocol for Server Actions - #8
Merged
Merged
Conversation
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>
…aude/action-failures # Conflicts: # CHANGELOG.md # packages/mutaflow/src/types.ts
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.
Server
actionFailure({ code, message, fieldErrors, formErrors })returns a plain serializable object. The keys offieldErrorsare checked against the input type.throw new FlowActionError(...)works from deeper code.withFlowContract, orwithActionFailureswhen there is no contract, returns it as a failure. Other errors are rethrown unchanged.withFlowContractinvalidates nothing for a failure, because the mutation did not happen.Client
runFlowturns a returned failure into aFlowActionErrorwith the samecode,fieldErrorsandformErrors. 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.FlowConfig.actionmay resolve toActionFailure, and the flow'sTResultexcludes it.resultstaysTodoinreconcile,redirectanddata, for both adapters and plain functions.useFlowFormmaps the failure tofieldErrors/formErrors. A failure that has only a message becomes a form error.captureError: truecaptures them again.Behaviour changes (CHANGELOG → Changed)
withFlowContractnow returnsTResult | ActionFailure<TInput>.resolveFlowActionandFlowConfig.actionallow resolving to anActionFailure.Test plan
npm run typecheckfrom a clean build. Newtests/types/failures.tschecks that:TResultfor adapters and plain actions;fieldErrorskeys are typed from the input;any;withFlowContractandwithActionFailuresreturn types are correct.npm test: 61/61. New tests cover:FlowActionErrorand back, and JSON serialization;runFlowrolls back, does not retry, and emits the expected events;withFlowContractskips invalidation for returned and thrown failures and rethrows unexpected errors;useFlowFormshows field and form errors;captureError: true.examples/next-app: typecheck andnext build.next build && next start) in a browser:"Read Mutaflow source" already exists;🤖 Generated with Claude Code