Skip to content

Commit

Permalink
catch error in gen future
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 25, 2024
1 parent 07bfa82 commit 3e72bf5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions examples/poke_api/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import 'package:poke_api/pokemon.dart';
import 'package:poke_api/pokemon_error.dart';

abstract interface class HttpClient {
Effect<E, PokemonError, String> get<E>(Uri uri);
Effect<Null, PokemonError, String> get(Uri uri);
}

class Http implements HttpClient {
@override
Effect<E, PokemonError, String> get<E>(Uri uri) => Effect.gen(
Effect<Null, PokemonError, String> get(Uri uri) => Effect.gen(
($) async {
final response = await $.async(Effect.tryCatch(
execute: () => http.get(uri),
Expand Down Expand Up @@ -42,7 +42,7 @@ Effect<Env, PokemonError, Pokemon> program(
}

final uri = Uri.parse(Constants.requestAPIUrl(id));
final body = await $.async(client.get(uri));
final body = await $.async(client.get(uri).withEnv());

final bodyJson = $.sync(
Either.tryCatch(
Expand Down
11 changes: 6 additions & 5 deletions packages/fpdart/lib/src/extension/future_or_extension.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'dart:async';

extension FutureOrThenExtension<A> on FutureOr<A> {
FutureOr<B> then<B>(FutureOr<B> Function(A a) f,
{B Function(Object error, StackTrace stackTrace)? onError}) {
FutureOr<B> then<B>(
FutureOr<B> Function(A a) f, {
B Function(Object error, StackTrace stackTrace)? onError,
}) {
switch (this) {
case Future<A> self:
return self.then(
f,
onError: (Object error, StackTrace stackTrace) {
return self.then(f).catchError(
(Object error, StackTrace stackTrace) {
if (onError != null) onError(error, stackTrace);
},
);
Expand Down

0 comments on commit 3e72bf5

Please sign in to comment.