Skip to content

Commit

Permalink
Added catch block if can't create a platform client (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkkiller committed Jun 28, 2024
1 parent 1732136 commit 0e3afe1
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/src/core/rest_client/src/http/rest_client_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:http/http.dart' as http;
import 'package:sizzle_starter/src/core/rest_client/rest_client.dart';
import 'package:sizzle_starter/src/core/rest_client/src/http/check_exception_io.dart'
if (dart.library.js_interop) 'package:sizzle_starter/src/core/rest_client/src/http/check_exception_browser.dart';
import 'package:sizzle_starter/src/core/utils/refined_logger.dart';

// coverage:ignore-start
/// Creates an [http.Client] based on the current platform.
Expand All @@ -14,17 +15,25 @@ import 'package:sizzle_starter/src/core/rest_client/src/http/check_exception_io.
/// For iOS and macOS, it returns a [CupertinoClient]
/// with the default session configuration.
http.Client createDefaultHttpClient() {
http.Client? client;
final platform = defaultTargetPlatform;

if (platform == TargetPlatform.android) {
return CronetClient.defaultCronetEngine();
}

if (platform == TargetPlatform.iOS || platform == TargetPlatform.macOS) {
return CupertinoClient.defaultSessionConfiguration();
try {
if (platform == TargetPlatform.android) {
client = CronetClient.defaultCronetEngine();
}
if (platform == TargetPlatform.iOS || platform == TargetPlatform.macOS) {
client = CupertinoClient.defaultSessionConfiguration();
}
} on Object catch (e, stackTrace) {
logger.warn(
'Failed to create a default http client for platform $platform',
error: e,
stackTrace: stackTrace,
);
}

return http.Client();
return client ?? http.Client();
}
// coverage:ignore-end

Expand Down

0 comments on commit 0e3afe1

Please sign in to comment.