Full-stack failure cases
What you need to know
Failure cases are part of the application contract, not exceptions to its design. Validate input, check access, compare the expected revision and commit the write as one coherent operation. A failed operation must not leave the database in an unintended state.
By the end, you can: design validation, database and access failures; verify consistent state.
Validate a title without converting a file to text
function readTitle(value:unknown) { if(typeof value!=='string') return null; const title=value.trim(); return title.length>=3 && title.length<=80 ? title : null;}console.log(readTitle(' Fern walk '));console.log(readTitle(' x '));Expected behavior: Fern walk, then null. Non-string inputs are rejected rather than stringified.
How it works
The editorial fixture validates titles between three and eighty trimmed characters and positive integer revisions. Unexpected database failures are different from expected domain failures: roll back the transaction, return a generic failure or allow the error boundary to handle it, and record a safe event code. The local SQLite fixture deliberately causes a constraint violation after an update inside a transaction; the test checks that the original title and revision are restored. An update that matches no rows can mean stale revision, missing resource or lost permission; do not reveal a conflict until the resource has been authorized. Prefer a conditional write with tenant/owner/revision constraints. For concurrent access, the exact transactional isolation and failure handling depend on the database; a single in-memory SQLite connection does not prove PostgreSQL behavior. Keep the UI's optimistic value separate from the last confirmed state and do not label a rejected edit as saved. A network timeout after submitting an edit is not evidence that the transaction rolled back. If the result is unknown, reconcile using the authoritative revision or a durable idempotency record before retrying; never create a second operation blindly.
Specify a rollback scenario
Initial: article a1, title Fern walk, revision 1.Transaction: rename to Moss walk; insert an audit record.Injected fixture failure: audit insert violates a unique key.Expected: rollback; title Fern walk, revision 1; audit count unchanged.Expected behavior: All effects are rolled back together in the local database model. The actual SQLite test uses synthetic data and a unique audit key.
Common mistake
Catch-all code that returns saved even when persistence throws hides a failure and may cause the user to lose work. Distinguish invalid, forbidden, conflict and unexpected errors; do not copy exception internals into a client response.
Practice and verification
Run the SQLite fixture tests. Then propose two integration requests with the same expected revision and assert only one can commit. Record the actual database, isolation and evidence before making a concurrency claim.
Scope: App Router, proposed Next.js 16.x and React 19, Cache Components disabled. Framework snippets are staged, not executed; local policy, SQLite and HTTP transport evidence is separate. Exact patched dependencies and platform contracts are pending.
Further reading: Actions · Auth · Security Data