Skip to content

Commit

Permalink
onError for gen
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 21, 2024
1 parent 7f3314e commit a63aae1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
6 changes: 4 additions & 2 deletions examples/poke_api/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ Effect<Env, PokemonError, Pokemon> program(
});

void main() async {
await program("721")
final exit = await program("72jj1")
.map((pokemon) => print(pokemon))
.catchError(
(error) => Effect.function(
() => print("No pokemon: $error"),
),
)
.runFuture((Http(), JsonCodec()));
.runFutureExit((Http(), JsonCodec()));

print(exit);
}
21 changes: 11 additions & 10 deletions packages/fpdart/lib/src/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:async';
import 'package:fpdart/fpdart.dart';
import 'package:fpdart/src/extension/future_or_extension.dart';
import 'package:fpdart/src/extension/iterable_extension.dart';
import 'package:meta/meta.dart';

import 'unit.dart' as fpdart_unit;

Expand All @@ -15,7 +14,7 @@ typedef EffectGen<E, L> = ({
A Function<A>(IEffect<E, L, A>) sync,
});

final class _EffectThrow<L> {
final class _EffectThrow<L> implements Exception {
final Cause<L> cause;
const _EffectThrow(this.cause);

Expand All @@ -29,7 +28,7 @@ EffectGen<E, L> _effectGen<E, L>(E? env) => (
async: <A>(effect) => Future.sync(
() => effect.asEffect._unsafeRun(env).then(
(exit) => switch (exit) {
Left(value: final cause) => throw _EffectThrow(cause),
Left(value: final cause) => throw _EffectThrow<L>(cause),
Right(value: final value) => value,
},
),
Expand All @@ -43,7 +42,7 @@ EffectGen<E, L> _effectGen<E, L>(E? env) => (
}

return switch (run) {
Left(value: final cause) => throw _EffectThrow(cause),
Left(value: final cause) => throw _EffectThrow<L>(cause),
Right(value: final value) => value,
};
},
Expand Down Expand Up @@ -115,14 +114,16 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
Future<Exit<L, R>> runFutureExit(E env) async => _unsafeRun(env);

/// {@category constructors}
// ignore: non_constant_identifier_names
factory Effect.gen(DoFunctionEffect<E, L, R> f) => Effect<E, L, R>._(
(env) {
try {
return f(_effectGen<E, L>(env)).then(Right.new);
} on _EffectThrow<L> catch (err) {
return Left(err.cause);
}
return f(_effectGen<E, L>(env)).then(
Right.new,
onError: (dynamic error, dynamic stackTrack) {
if (error is _EffectThrow<L>) {
return Left<Cause<L>, R>(error.cause);
}
},
);
},
);

Expand Down
5 changes: 1 addition & 4 deletions packages/fpdart/lib/src/exit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import 'effect.dart';

typedef Exit<L, R> = Either<Cause<L>, R>;

sealed class Cause<L> {
sealed class Cause<L> implements Error {
const Cause();

StackTrace? get stackTrace;

Cause<L> withTrace(StackTrace stack);
}

Expand Down
5 changes: 3 additions & 2 deletions packages/fpdart/lib/src/extension/future_or_extension.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:async';

extension FutureOrThenExtension<A> on FutureOr<A> {
FutureOr<B> then<B>(FutureOr<B> Function(A a) f) => switch (this) {
final Future<A> self => self.then(f),
FutureOr<B> then<B>(FutureOr<B> Function(A a) f, {Function? onError}) =>
switch (this) {
final Future<A> self => self.then(f, onError: onError),
final A self => f(self),
};
}

0 comments on commit a63aae1

Please sign in to comment.