unthrown icon indicating copy to clipboard operation
unthrown copied to clipboard

Ergonomics of producing a deliberate Defect

Open btravers opened this issue 4 months ago • 0 comments

Summary

Design discussion: producing a deliberate Defect — short-circuiting a chain into the Defect channel for a known condition — is currently awkward, and it's not obvious what the blessed idiom is.

Motivation — real-world evidence

At an inbound HTTP boundary that folds a Defect to a 500, a known-technical precondition (a required config value is missing) must surface as a Defect. The only way found to mint that Defect was:

return fromThrowable(
  () => { throw new Error("required config value is missing"); },
  (cause, defect) => defect(cause),
)().toAsync();

That's a lot of ceremony (throw-inside-a-thunk + throwaway qualify + toAsync) for "this is a defect, put it in the Defect channel."

Constraint

This is deliberately in tension with Thesis #3 / the no-Defect-constructor rule: defects are supposed to arise only at boundaries, and there's intentionally no public Defect(...) constructor. So this issue is a design question, not a request to just add a constructor.

Options to weigh

  1. Do nothing but document a canonical idiom (and shorten it via fromSafeThrowable (see #76): fromSafeThrowable(() => { throw e })().toAsync()).
  2. A narrow, honestly-named boundary helper — e.g. something that replays a known cause through the fromThrowable boundary (the same mechanism @unthrown/effect's fromExit already uses to mint a Defect from Effect's die). Naming should make clear it's a boundary, not a general constructor.
  3. Explicitly decide it stays awkward on purpose and just document why, so future readers understand it's intentional friction.

Leaning toward (1) + a decision recorded in the guide, but opening for discussion.

btravers avatar Jul 08 '26 22:07 btravers