Effect CommunityEC
Effect Community3y ago
70 replies
timbotronic

Error Propagation in Generator Functions

Heya 🌊

Trying to get my head around error propagation and have a basic example. In my generator function i want it to fail fast if a user inputs an environment that doesnt exist.

I originally wrote:
const env = yield* _(Effect.try({
  try: () => globalConfig.environments.find(({ name }) => name === envName),
  catch: () => Effect.fail(new EnvironmentMissingError(envName)),
}))

which always passes. Writing the code instead as the following gives me results as expected (code fails fast and gives the correct error:

const result = globalConfig.environments.find(({ name }) => name === envName);
const env =
  result === undefined ? yield* _(Effect.fail(new EnvironmentMissingError(envName))) : result;


I much prefer the look of the first code block, where did i go wrong with it?
Was this page helpful?