Skip to content

Commit e654c14

Browse files
committed
Add trycall tests
1 parent b3be6ee commit e654c14

File tree

3 files changed

+694
-32
lines changed

3 files changed

+694
-32
lines changed

lib/common/result/result.dart

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,32 @@ sealed class Result<T> {
3939
_ => this as Result<R>,
4040
};
4141
}
42+
}
4243

43-
/// Executes the given API call [fn]. When the call succeeds, [Result.success] is returned with the response.
44-
/// When the call fails the optional [onError] is executed and the exceptions are handled.
45-
/// If there is no [onError] provided, an error of type [BaseError] is returned in [Result.failure].
46-
Future<Result<S>> tryCall<S>(
47-
FutureOr<S> Function() fn, {
48-
Future<Result<S>> Function(Exception error)? onError,
49-
}) async {
50-
try {
51-
return Result.success(await fn());
52-
} on Exception catch (e) {
53-
if (onError != null) {
54-
return onError(e);
55-
}
56-
return switch (e) {
57-
ChopperHttpException() => Result.failure(
58-
switch (e.response.statusCode) {
59-
401 => const AuthenticationFailedError(),
60-
_ => const ServerError(),
61-
},
62-
),
63-
ClientException() => Result.failure(const NoInternetError()),
64-
CheckedFromJsonException() => Result.failure(const ServerError()),
65-
_ => Result.failure(const UnknownError()),
66-
};
44+
/// Executes the given API call [fn]. When the call succeeds, [Result.success] is returned with the response.
45+
/// When the call fails the optional [onError] is executed and the exceptions are handled.
46+
/// If there is no [onError] provided, an error of type [BaseError] is returned in [Result.failure].
47+
Future<Result<S>> tryCall<S>(
48+
FutureOr<S> Function() fn, {
49+
Future<Result<S>> Function(Exception error)? onError,
50+
}) async {
51+
try {
52+
return Result.success(await fn());
53+
} on Exception catch (e) {
54+
if (onError != null) {
55+
return onError(e);
6756
}
57+
return switch (e) {
58+
ChopperHttpException() => Result.failure(
59+
switch (e.response.statusCode) {
60+
401 => const AuthenticationFailedError(),
61+
_ => const ServerError(),
62+
},
63+
),
64+
ClientException() => Result.failure(const NoInternetError()),
65+
CheckedFromJsonException() => Result.failure(const ServerError()),
66+
_ => Result.failure(const UnknownError()),
67+
};
6868
}
6969
}
7070

@@ -79,10 +79,7 @@ final class Success<T> extends Result<T> {
7979

8080
@override
8181
bool operator ==(Object other) {
82-
return identical(this, other) ||
83-
other is Success<T> &&
84-
runtimeType == other.runtimeType &&
85-
value == other.value;
82+
return identical(this, other) || other is Success<T> && runtimeType == other.runtimeType && value == other.value;
8683
}
8784

8885
@override
@@ -100,10 +97,7 @@ final class Failure<T> extends Result<T> {
10097

10198
@override
10299
bool operator ==(Object other) {
103-
return identical(this, other) ||
104-
other is Failure<T> &&
105-
runtimeType == other.runtimeType &&
106-
other.error == error;
100+
return identical(this, other) || other is Failure<T> && runtimeType == other.runtimeType && other.error == error;
107101
}
108102

109103
@override

0 commit comments

Comments
 (0)