unthrown icon indicating copy to clipboard operation
unthrown copied to clipboard

@unthrown/prisma: codes outside the modeled set silently become retryable in Temporal activities

Open btravers opened this issue 3 months ago • 1 comments

Summary

try* models a fixed set of Prisma error codes — P2002, P2003, P2018, P2025. Everything else lands on the Defect channel. That is a reasonable default, but it creates a sharp edge when migrating an existing adapter that hand-qualified a different code into a modeled error.

How it bites

Pre-migration, an adapter treated two codes as the same domain outcome:

// "no such record" was reachable two ways: a missing row, or a malformed id
if (isPrismaErrorNotFound(error) || isPrismaErrorInvalidInputSyntax(error)) {
  return Err(new RecordNotFoundError({ id }));
}

tryUpdate covers the P2025 half but not P2007 (malformed id / inconsistent column data). The mechanical migration therefore moves the malformed-id case from a deterministic Err to a Defect.

In a Temporal activity that is a behaviour change with teeth: Err converted via toNonRetryableApplicationFailure fails fast, whereas a Defect is rethrown as a plain error and Temporal retries it. If the caller is an error-handling path that passes through whatever id triggered it — an onError handler, a dead-letter reprocessor — the workflow retries forever on input that can never succeed.

Why it is hard to catch

Every signal points the wrong way:

  • type-check passes — the Err channel legitimately shrank
  • a unit test on the repository passes — it asserts the Ok path, or asserts a Defect and is satisfied
  • the failure surfaces as a test timeout in a workflow spec, which is indistinguishable at a glance from ordinary Temporal test flakiness

In our case it was separated from a flake only by reproducing in isolation: it disappeared with the fix, not with a re-run. A repository-level unit test would never have shown it — the path had to be driven through a workflow.

Suggestions

  1. Document the modeled set as exhaustive, next to the operator table, with the consequence stated: codes outside this set become Defects, and a Defect is retryable in a Temporal activity. Right now the table reads as "here are the errors you get", not "here is the boundary".
  2. A migration note: when replacing a hand-rolled qualifier, diff it against the modeled set — anything else it caught needs explicit re-qualification.
  3. Optional: an extension point for qualifying additional codes, so consumers do not each hand-roll .recoverDefect((cause) => isPrismaErrorX(cause) ? Err(...) : ...). A predicate/mapping passed to unthrownPrisma would keep the qualification next to the client rather than scattered across call sites.

(1) alone would have prevented this; (3) is a nice-to-have.

Workaround in use

.recoverDefect((cause) =>
  isPrismaErrorInvalidInputSyntax(cause)
    ? Err(new RecordNotFoundError({ id }))
    : /* leave it a Defect */ …,
)

with a comment naming why it must not stay a Defect — otherwise the next reader "simplifies" it back.

btravers avatar Aug 12 '26 16:08 btravers

Good report — the "every signal points the wrong way" section is the useful part, and it's now in the docs.

Taking (1) and (2). #228:

  • The README and the guide now state, right next to the per-operation table, that P2002/P2003/P2018/P2025 are the whole modeled set — with P2007, P2023, P2000, P2011, P2015 named as examples of codes that read like domain outcomes and are not. The table was reading as a menu; it's a boundary.
  • The consequence is spelled out where you hit it: folded at an HTTP edge a defect is a 500 and the request is over, but under a supervisor that retries on a thrown error — a Temporal activity — an Err converted to a non-retryable failure fails fast while a defect is retried, so a malformed id retries forever on input that can never succeed.
  • New guide section, Migrating a hand-rolled qualifier: diff the old qualifier against the four codes and re-qualify the rest with recoverDefect, rethrowing what you did not name (the throw cause goes through the pipeline's own throw-to-defect net, so untouched causes stay defects with their original value). It also records why the migration is silent — the type check passes because the Err channel legitimately shrank, and a repository unit test on the Ok path passes too.

The example re-qualifies into the RecordNotFound the operation already models, so E is unchanged and no call site grows an arm — which is your case exactly. Note it detects the code structurally (name === "PrismaClientKnownRequestError" + a string code) rather than with instanceof: Prisma's runtime module path moves between majors, and that's the check the extension itself uses.

Not taking (3), the qualifier extension point on unthrownPrisma — recoverDefect at the call site already expresses it, and threading caller-supplied codes into each operation's E is real inference complexity for a case documentation covers. If you find the recoverDefect wrapper genuinely doesn't scale across a repo, reopen with what it looks like at N call sites and I'll reconsider.

btravers avatar Aug 12 '26 17:08 btravers