Open effect-bot opened 3 weeks ago
I would like a refineOrDie
method which refines the error channel or dies based on custom logic such as refinements. This is useful for cases where I do not have a tagged error and wish to map an E to some E1 or die.
Something like so:
export const refineErrorOrDie =
<A, E, E1>(f: (e: E) => Option.Option<E1>) =>
(self: Effect.Effect<A, E>): Effect.Effect<A, E1> =>
self.pipe(
Effect.catchAll((e) =>
Option.match(f(e), {
onNone: () => Effect.die(e),
onSome: Effect.fail
})
)
)
I can accomplish the same thing with a catchAll but it requires some type annotations which isn't ideal:
Effect.catchAll((e) => {
if (isUniqueConstraintError(e)) {
return Effect.fail<Chip.AlreadyExists | Team.NotFound>(new Chip.AlreadyExists())
}
if (isForegienKeyConstraintError(e)) {
return Effect.fail<Chip.AlreadyExists | Team.NotFound>(new Team.NotFound())
}
return Effect.die(e)
})
You can use Effect.catchIf(isSomeError, Effect.die)
Summary
Summary
The conversation revolves around refining error channels in TypeScript Effects without using tagged errors. The initial code snippet provided by the user demonstrates a custom combinator
refineErrorOrDie
that refines the error channel by mapping specific errors to custom tagged errors or dying otherwise. The user also mentions an alternative approach usingEffect.catchAll
, which requires type annotations and is not ideal.Another participant suggests using
.catch
, but the user clarifies that they want to use refinements instead of discriminators. The conversation touches on the idea of usingmapError
,flip
,filterOrDie
, andflip
, but this approach is deemed ugly. The user mentions that there used to be arefineOrDie
function that was removed for being unsafe and misleading.The user expresses a preference for a single combinator to handle error refinement rather than flipping between the A/E channels. The suggestion is made to create an issue requesting the return of
refineErrorOrDie
andrefineErrorTagOrDie
.Key Takeaways
refineErrorOrDie
for refining error channels.Effect.catchAll
with type annotations is an alternative but not ideal.refineOrDie
function that was removed for safety concerns.refineErrorOrDie
andrefineErrorTagOrDie
.Action Items
refineErrorOrDie
andrefineErrorTagOrDie
in the Effect-TS library.Discord thread
https://discord.com/channels/795981131316985866/1252232384070422598