Concerns Over Handling Errors in Generators and Resulting Types

i think i have an incomplete understanding of generators. i was under the impression in this instance that i've actually handled any possible errors within the generator. so why would the result before its resolved by the runtime still be Effect<Value, NoSuchElementException, never>? Im paranoid this will still blow up given such an exception despite having handled all occurances of the error?

export function generateSummary({ aggregate, data, property }: GenerateSummaryArgs) {
  // Effect<Value, NoSuchElementException, never>
  const result = Effect.gen(function* ($) {
    const summary = yield* $(
      ReadonlyRecord.get(propertyMap, property),
      Effect.tapError(Console.warn),
      Effect.option,
    );

    if (Option.isNone(summary)) {
      return {
        content: "An error has occured. The metric data you've requested does not exist.",
        title: "Metric Comparison",
      };
    }

    const comparative = yield* $(
      data.percentageChange,
      Effect.fromNullable,
      Effect.map((value) => (value <= 0 ? `${Math.abs(value)}% less` : `${Math.abs(value)}% more`)),
      Effect.option,
    );

    if (Option.isNone(comparative)) {
      return yield *$(summary, Effect.map(({title}) => ({
        content: "There is not enough data available to present this comparison.",
        title,
      })));
    }

    return yield* $(
      Effect.all({ summary, comparative }),
      Effect.map((result) => ({
        title: result.summary.title,
        content: [
          `You have ${result.summary.adjective}`,
          `${result.comparative} ${result.summary.pluralUnit}`,
          `today than this time last ${aggregate.toString()}.`,
        ].join(" "),
      })),
    );
  });

  return result.pipe(Effect.runSync)
}
Was this page helpful?