Skip to content

Commit e82dba6

Browse files
catchError and catchCause
1 parent a63aae1 commit e82dba6

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

examples/poke_api/lib/main.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Effect<Env, PokemonError, Pokemon> program(
3737
PokemonIdNotInt.new,
3838
));
3939

40-
if (id < Constants.minimumPokemonId && id > Constants.maximumPokemonId) {
40+
if (id < Constants.minimumPokemonId || id > Constants.maximumPokemonId) {
4141
return $.sync(Effect.fail(const InvalidPokemonIdRange()));
4242
}
4343

@@ -65,9 +65,9 @@ Effect<Env, PokemonError, Pokemon> program(
6565
});
6666

6767
void main() async {
68-
final exit = await program("72jj1")
68+
final exit = await program("9722")
6969
.map((pokemon) => print(pokemon))
70-
.catchError(
70+
.catchError<void>(
7171
(error) => Effect.function(
7272
() => print("No pokemon: $error"),
7373
),

packages/fpdart/lib/src/effect.dart

+33-5
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,29 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
103103

104104
/// {@category execution}
105105
Future<R> runFuture(E env) async {
106-
final result = await _unsafeRun(env);
107-
return switch (result) {
106+
final result = _unsafeRun(env);
107+
if (result is! Future) {
108+
print(
109+
"You can use runSync instead of runFuture since the Effect is synchronous",
110+
);
111+
}
112+
113+
return switch (await result) {
108114
Left(value: final cause) => throw cause,
109115
Right(value: final value) => value,
110116
};
111117
}
112118

113119
/// {@category execution}
114-
Future<Exit<L, R>> runFutureExit(E env) async => _unsafeRun(env);
120+
Future<Exit<L, R>> runFutureExit(E env) async {
121+
final result = _unsafeRun(env);
122+
if (result is! Future) {
123+
print(
124+
"You can use runSyncExit instead of runFutureExit since the Effect is synchronous",
125+
);
126+
}
127+
return result;
128+
}
115129

116130
/// {@category constructors}
117131
factory Effect.gen(DoFunctionEffect<E, L, R> f) => Effect<E, L, R>._(
@@ -366,8 +380,8 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
366380
);
367381

368382
/// {@category error_handling}
369-
Effect<E, Never, R> catchError(
370-
Effect<E, Never, R> Function(L error) f,
383+
Effect<E, C, R> catchError<C>(
384+
Effect<E, C, R> Function(L error) f,
371385
) =>
372386
Effect._(
373387
(env) => _unsafeRun(env).then(
@@ -382,6 +396,20 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
382396
},
383397
),
384398
);
399+
400+
/// {@category error_handling}
401+
Effect<E, C, R> catchCause<C>(
402+
Effect<E, C, R> Function(Cause<L> cause) f,
403+
) =>
404+
Effect._(
405+
(env) => _unsafeRun(env).then(
406+
(exit) => switch (exit) {
407+
Left(value: final cause) => f(cause),
408+
Right(value: final value) => Effect<E, C, R>.succeed(value),
409+
}
410+
._unsafeRun(env),
411+
),
412+
);
385413
}
386414

387415
extension ProvideNever<L, R> on Effect<Never, L, R> {

0 commit comments

Comments
 (0)