Skip to content

Commit

Permalink
Remove internal EffectError make sure errors are raised with Effect.f…
Browse files Browse the repository at this point in the history
…ail in Effect.try (#4202)
  • Loading branch information
mikearnaldi authored Dec 29, 2024
1 parent 8b46be6 commit 0988083
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-scissors-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Remove internal EffectError make sure errors are raised with Effect.fail in Effect.try
8 changes: 3 additions & 5 deletions packages/effect/src/internal/core-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,11 @@ export const try_: {
evaluate = arg.try
onFailure = arg.catch
}
return core.sync(() => {
return core.suspend(() => {
try {
return evaluate()
return core.succeed(internalCall(evaluate))
} catch (error) {
throw core.makeEffectError(internalCause.fail(
onFailure ? onFailure(error) : new core.UnknownException(error)
))
return core.fail(onFailure ? internalCall(() => onFailure(error)) : new core.UnknownException(error))
}
})
}
Expand Down
26 changes: 0 additions & 26 deletions packages/effect/src/internal/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,6 @@ import { SingleShotGen } from "./singleShotGen.js"
// Effect
// -----------------------------------------------------------------------------

/** @internal */
const EffectErrorSymbolKey = "effect/EffectError"

/** @internal */
export const EffectErrorTypeId = Symbol.for(EffectErrorSymbolKey)

/** @internal */
export type EffectErrorTypeId = typeof EffectErrorTypeId

/** @internal */
export interface EffectError<out E> {
readonly [EffectErrorTypeId]: EffectErrorTypeId
readonly _tag: "EffectError"
readonly cause: Cause.Cause<E>
}

/** @internal */
export const isEffectError = (u: unknown): u is EffectError<unknown> => hasProperty(u, EffectErrorTypeId)

/** @internal */
export const makeEffectError = <E>(cause: Cause.Cause<E>): EffectError<E> => ({
[EffectErrorTypeId]: EffectErrorTypeId,
_tag: "EffectError",
cause
})

/**
* @internal
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/effect/src/internal/fiberRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ export interface Snapshot {
flags: RuntimeFlags.RuntimeFlags
}

const _version = version.getCurrentVersion()

/** @internal */
export class FiberRuntime<in out A, in out E = never> extends Effectable.Class<A, E>
implements Fiber.RuntimeFiber<A, E>
Expand Down Expand Up @@ -1367,7 +1369,7 @@ export class FiberRuntime<in out A, in out E = never> extends Effectable.Class<A
// @ts-expect-error
cur = this.currentTracer.context(
() => {
if (version.getCurrentVersion() !== (cur as core.Primitive)[core.EffectTypeId]._V) {
if (_version !== (cur as core.Primitive)[core.EffectTypeId]._V) {
return core.dieMessage(
`Cannot execute an Effect versioned ${
(cur as core.Primitive)[core.EffectTypeId]._V
Expand Down Expand Up @@ -1400,8 +1402,6 @@ export class FiberRuntime<in out A, in out E = never> extends Effectable.Class<A
} catch (e) {
if (cur !== YieldedOp && !Predicate.hasProperty(cur, "_op") || !((cur as core.Primitive)._op in this)) {
cur = core.dieMessage(`Not a valid effect: ${Inspectable.toStringUnknown(cur)}`)
} else if (core.isEffectError(e)) {
cur = core.exitFailCause(e.cause)
} else if (core.isInterruptedException(e)) {
cur = core.exitFailCause(
internalCause.sequential(internalCause.die(e), internalCause.interrupt(FiberId.none))
Expand Down

0 comments on commit 0988083

Please sign in to comment.