safeCall<F, T> method
Future<Either<F, T>>
safeCall<
F, T>({ - required Future<T> call(),
- required F onNetworkError(),
- required F onResponseError(
- Response? response
),
- required F onUnknownError(
- Exception e
),
})
Implementation
@protected
Future<Either<F, T>> safeCall<F, T>({
required Future<T> Function() call,
required F Function() onNetworkError,
required F Function(Response? response)
onResponseError, // ignore: always_specify_types
required F Function(Exception e) onUnknownError,
}) async {
try {
final T result = await call();
return right(result);
} on DioException catch (e) {
// logger.e("BaseService.safeCall: ", e);
switch (e.type) {
case DioExceptionType.connectionTimeout:
return left(onNetworkError());
case DioExceptionType.sendTimeout:
return left(onNetworkError());
case DioExceptionType.receiveTimeout:
return left(onNetworkError());
case DioExceptionType.badResponse:
return left(onResponseError(e.response));
case DioExceptionType.cancel:
return left(onNetworkError());
case DioExceptionType.unknown:
if (e.error is SocketException) {
return left(onNetworkError());
}
return left(onUnknownError(e));
case DioExceptionType.badCertificate:
return left(onNetworkError());
case DioExceptionType.connectionError:
return left(onNetworkError());
case DioExceptionType.unknown:
return left(onNetworkError());
}
}
}