Effect CommunityEC
Effect Community3y ago
49 replies
Victor Korzunin

Defining Errors in Service Methods

How one defines errors in the service methods in generic way? So I can provide different implementations for the same Tag. Example:
import { Effect, Context, Layer } from "effect"
import { Flour, Sugar } from "./Ingredients"
 
export interface Recipe {
  readonly steps: Effect.Effect<never, ???, ReadonlyArray<string>>
}
 
export const Recipe = Context.Tag<Recipe>()
 
// $ExpectType Layer<Sugar | Flour, never, Recipe>
export const Recipe1Live = Layer.effect(
  Recipe,
  Effect.gen(function* (_) {
    const sugar = yield* _(Sugar)
    const flour = yield* _(Flour)
    return Recipe.of({
      // $ExpectType Effect<never, never, ReadonlyArray<string>>
      steps: Effect.all([sugar.grams(200), flour.cups(1)])
    })
  })
)

// $ExpectType Layer<Eggs, never, Recipe>
export const Recipe2Live = Layer.effect(
  Recipe,
  Effect.gen(function* (_) {
    const flour = yield* _(Eggs)
    return Recipe.of({
      // $ExpectType Effect<never, NoEggsError, ReadonlyArray<string>>
      steps: Effect.fail(new NoEggsError())
    })
  })
)
Was this page helpful?