diff --git a/examples/poke_api/lib/main.dart b/examples/poke_api/lib/main.dart index 6ca46e0..3c24ebf 100644 --- a/examples/poke_api/lib/main.dart +++ b/examples/poke_api/lib/main.dart @@ -37,7 +37,7 @@ Effect program( PokemonIdNotInt.new, )); - if (id < Constants.minimumPokemonId && id > Constants.maximumPokemonId) { + if (id < Constants.minimumPokemonId || id > Constants.maximumPokemonId) { return $.sync(Effect.fail(const InvalidPokemonIdRange())); } @@ -65,9 +65,9 @@ Effect program( }); void main() async { - final exit = await program("72jj1") + final exit = await program("9722") .map((pokemon) => print(pokemon)) - .catchError( + .catchError( (error) => Effect.function( () => print("No pokemon: $error"), ), diff --git a/packages/fpdart/lib/src/effect.dart b/packages/fpdart/lib/src/effect.dart index af6f42f..076fcee 100644 --- a/packages/fpdart/lib/src/effect.dart +++ b/packages/fpdart/lib/src/effect.dart @@ -103,15 +103,29 @@ final class Effect extends IEffect { /// {@category execution} Future runFuture(E env) async { - final result = await _unsafeRun(env); - return switch (result) { + final result = _unsafeRun(env); + if (result is! Future) { + print( + "You can use runSync instead of runFuture since the Effect is synchronous", + ); + } + + return switch (await result) { Left(value: final cause) => throw cause, Right(value: final value) => value, }; } /// {@category execution} - Future> runFutureExit(E env) async => _unsafeRun(env); + Future> runFutureExit(E env) async { + final result = _unsafeRun(env); + if (result is! Future) { + print( + "You can use runSyncExit instead of runFutureExit since the Effect is synchronous", + ); + } + return result; + } /// {@category constructors} factory Effect.gen(DoFunctionEffect f) => Effect._( @@ -366,8 +380,8 @@ final class Effect extends IEffect { ); /// {@category error_handling} - Effect catchError( - Effect Function(L error) f, + Effect catchError( + Effect Function(L error) f, ) => Effect._( (env) => _unsafeRun(env).then( @@ -382,6 +396,20 @@ final class Effect extends IEffect { }, ), ); + + /// {@category error_handling} + Effect catchCause( + Effect Function(Cause cause) f, + ) => + Effect._( + (env) => _unsafeRun(env).then( + (exit) => switch (exit) { + Left(value: final cause) => f(cause), + Right(value: final value) => Effect.succeed(value), + } + ._unsafeRun(env), + ), + ); } extension ProvideNever on Effect {