Skip to content

Commit

Permalink
make Exit.failCause refails work
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Sep 13, 2024
1 parent 1de3c36 commit bde540b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
27 changes: 17 additions & 10 deletions packages/effect/src/internal/cause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ export const fail = <E>(error: E): Cause.Cause<E> => {
const o = Object.create(proto)
o._tag = OpCodes.OP_FAIL
o.error = error
return o
return rehydrateAnnotations(o, error)
}

/** @internal */
export const die = (defect: unknown): Cause.Cause<never> => {
const o = Object.create(proto)
o._tag = OpCodes.OP_DIE
o.defect = defect
return o
return rehydrateAnnotations(o, defect)
}

/** @internal */
Expand Down Expand Up @@ -136,15 +136,19 @@ export const annotated = dual<
(context: Context.Context<never>) => <E>(self: Cause.Cause<E>) => Cause.Cause<E>,
<E>(self: Cause.Cause<E>, context: Context.Context<never>) => Cause.Cause<E>
>(2, (self, context) => {
if (self._tag === OpCodes.OP_ANNOTATED && self.context === context) {
return self
}
const o = Object.create(proto)
o._tag = OpCodes.OP_ANNOTATED
if (self._tag === OpCodes.OP_ANNOTATED) {
o.context = Context.merge(context, self.context)
o.cause = propagateAnnotations(self.cause, o.context)
o.cause = self.cause
} else {
o.context = context
o.cause = propagateAnnotations(self, context)
o.cause = self
}
propagateAnnotations(o.cause, o.context)
return o
})

Expand Down Expand Up @@ -1321,16 +1325,19 @@ function addOriginalAnnotations<E>(obj: E, annotations: Context.Context<never>):
})
}

const propagateAnnotations = <E>(self: Cause.Cause<E>, context: Context.Context<never>): Cause.Cause<E> => {
const rehydrateAnnotations = <E>(self: Cause.Cause<E>, obj: unknown): Cause.Cause<E> => {
const annotations = originalAnnotations(obj)
return annotations ? annotated(self, annotations) : self
}

const propagateAnnotations = <E>(self: Cause.Cause<E>, context: Context.Context<never>): void => {
switch (self._tag) {
case "Die": {
return die(addOriginalAnnotations(self.defect, context))
;(self as any).defect = addOriginalAnnotations(self.defect, context)
break
}
case "Fail": {
return fail(addOriginalAnnotations(self.error, context))
}
default: {
return self
;(self as any).error = addOriginalAnnotations(self.error, context)
}
}
}
Expand Down
20 changes: 6 additions & 14 deletions packages/effect/src/internal/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,21 +662,15 @@ export const checkInterruptible = <A, E, R>(
f: (isInterruptible: boolean) => Effect.Effect<A, E, R>
): Effect.Effect<A, E, R> => withFiberRuntime((_, status) => f(_runtimeFlags.interruption(status.runtimeFlags)))

const capture = <E>(cause: Cause.Cause<E>, obj?: unknown): Effect.Effect<never, E> =>
const capture = <E>(cause: Cause.Cause<E>): Effect.Effect<never, E> =>
withFiberRuntime((fiber) => {
const originalAnnotations = internalCause.originalAnnotations(obj)
if (originalAnnotations) {
cause = internalCause.annotated(cause, originalAnnotations)
} else {
const span = currentSpanFromFiber(fiber)
let context = Context.empty()
if (span._tag === "Some") {
context = Context.add(context, internalCause.FailureSpan, span.value)
}
cause = Context.isEmpty(context) ? cause : internalCause.annotated(cause, context)
const span = currentSpanFromFiber(fiber)
let context = Context.empty()
if (span._tag === "Some") {
context = Context.add(context, internalCause.FailureSpan, span.value)
}
const effect = new EffectPrimitiveFailure(OpCodes.OP_FAILURE) as any
effect.effect_instruction_i0 = cause
effect.effect_instruction_i0 = Context.isEmpty(context) ? cause : internalCause.annotated(cause, context)
return effect
})

Expand Down Expand Up @@ -714,9 +708,7 @@ export const failSync = <E>(evaluate: LazyArg<E>): Effect.Effect<never, E> => fl
export const failCause = <E>(cause: Cause.Cause<E>): Effect.Effect<never, E> => {
switch (cause._tag) {
case "Fail":
return capture(cause, cause.error)
case "Die":
return capture(cause, cause.defect)
case "Interrupt":
return capture(cause)
default: {
Expand Down

0 comments on commit bde540b

Please sign in to comment.