MCPcopy Create free account
hub / github.com/Effect-TS/effect / handleForbidden

Function handleForbidden

packages/effect/src/ParseResult.ts:1627–1671  ·  view source on GitHub ↗
(
  effect: Effect.Effect<A, ParseIssue, R>,
  ast: AST.AST,
  actual: unknown,
  options: InternalOptions | undefined
)

Source from the content-addressed store, hash-verified

1625const dropRightRefinement = (ast: AST.AST): AST.AST => AST.isRefinement(ast) ? dropRightRefinement(ast.from) : ast
1626
1627const handleForbidden = <A, R>(
1628 effect: Effect.Effect<A, ParseIssue, R>,
1629 ast: AST.AST,
1630 actual: unknown,
1631 options: InternalOptions | undefined
1632): Effect.Effect<A, ParseIssue, R> => {
1633 // If effects are allowed, return the original effect
1634 if (options?.isEffectAllowed === true) {
1635 return effect
1636 }
1637
1638 // If the effect is already an Either, return it directly
1639 if (isEither(effect)) {
1640 return effect
1641 }
1642
1643 // Otherwise, attempt to execute the effect synchronously
1644 const scheduler = new Scheduler.SyncScheduler()
1645 const fiber = Effect.runFork(effect as Effect.Effect<A, ParseIssue>, { scheduler })
1646 scheduler.flush()
1647 const exit = fiber.unsafePoll()
1648
1649 if (exit) {
1650 if (Exit.isSuccess(exit)) {
1651 // If the effect successfully resolves, wrap the value in a Right
1652 return Either.right(exit.value)
1653 }
1654 const cause = exit.cause
1655 if (Cause.isFailType(cause)) {
1656 // The effect executed synchronously but failed due to a ParseIssue
1657 return Either.left(cause.error)
1658 }
1659 // The effect executed synchronously but failed due to a defect (e.g., a missing dependency)
1660 return Either.left(new Forbidden(ast, actual, Cause.pretty(cause)))
1661 }
1662
1663 // The effect could not be resolved synchronously, meaning it performs async work
1664 return Either.left(
1665 new Forbidden(
1666 ast,
1667 actual,
1668 "cannot be be resolved synchronously, this is caused by using runSync on an effect that performs async work"
1669 )
1670 )
1671}
1672
1673const compare = ([a]: [number, ...Array<unknown>], [b]: [number, ...Array<unknown>]) => a > b ? 1 : a < b ? -1 : 0
1674

Callers 2

goMemoFunction · 0.85
goFunction · 0.85

Calls 3

flushMethod · 0.95
unsafePollMethod · 0.65
isEitherFunction · 0.50

Tested by

no test coverage detected