pigoz/effect-crashcourse

Ensure handling all errors

steida opened this issue · 4 comments

It would be nice to add an example demonstrating handling all errors with an exhaustive switch (or is Match better?)

pigoz commented

Right, I'm actually not sure what the prescribed way is. In my app I have a function that takes in Effect<R, never, A> and passes it to the Runtime to generate a Promise.

You can do exaustive switchs with Match, but I believe something leveraging catchTags would be better? i.e.:

pipe(
  Effect.fail({ _tag: "foo" as const }),
  Effect.catchTag("foo", (_) => Effect.die(_)),
  Effect.mapError<never, never>(identity) // <- I wonder if this should be called Effect.exaustive
);

@pigoz Maybe match?

const p = pipe(
  sync(req),
  Effect.provideService(DbTag, db),
  Effect.match(
    (e) => {
      //
    },
    (a) => {
      //
    }
  )
);

Maybe runCallback is better, but then how to handle Exit with all known and unknown errors?

pigoz commented

I thinks it's something that depends on your app. Like, if your Effects are ultimately converted into Promises, you can make a runPromiseExaustive function:

function runPromiseExaustive<A>(effect: Effect.Effect<never, never, A>) {
  return Effect.runPromise(effect);
}

function runtimeRunPromiseExaustive(runtime: Runtime.Runtime<R>, effect: Effect.Effect<R, never, A>) {
  return runtime.runPromise(effect)
}

That's the approach I'm using currently, since I'm using Effect in the server side parts of Remix (it's actually slightly more contrived because I'm allowing some errors to go through since Remix catches subclasses of Response to handle navigation). Also if you are splitting Effect creation inside functions you can specify E as never as the return type.

But a way to assert there's no unhandled failures in a pipe might be useful.