Effect CommunityEC
Effect Communityโ€ข2y agoโ€ข
38 replies
imagio

Handling Unhandled Failures in Long Running Effects

What's a good way to deal with potential "unhandled failures" when forking long running effects? I have several places where I fork effects that wait for values from a Stream for the entire life of the app (like auth uid change from an external lib). If there's a failure in one of these forked effects it's a silent failure unless I ensure that I tap/catch error because I never join them to check the exit value. Is there any way to at least warn when this happens? It's somewhat similar to an unhandled promise rejection. Example:

import { Effect, SubscriptionRef, Stream, Fiber, pipe, Schedule } from 'effect';
import * as Schema from '@effect/schema/Schema';

class SomeError extends Schema.TaggedError<SomeError>()('SomeError', {}) {}

const program = Effect.gen(function* (_) {
  const fail = pipe(
    Effect.sleep(`2 seconds`),
    Effect.andThen(() => Effect.fail(new SomeError({})))
  );
  const forked = yield* _(
    fail,
    Effect.tapErrorCause(Effect.logError),
    Effect.forkScoped
  );
  yield* _(Effect.sleep(`4 seconds`));
  return 'done';
}).pipe(Effect.scoped);

Effect.runPromise(program).then(console.log).catch(console.error);


If you remove the tapErrorCause you'd never know that the forked effect failed.
Was this page helpful?