Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing connection error #1912

Merged
merged 16 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ See the [Migration Guide][] for the complete breaking changes list.**
## Unreleased

*None.*
## 5.3.2

- Fixing requests failing with unknown instead of connection error on SocketException
## 5.3.1

- Improve package descriptions and code formats.
Expand Down
6 changes: 5 additions & 1 deletion dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ class IOHttpClientAdapter implements HttpClientAdapter {
});
} on SocketException catch (e) {
if (!e.message.contains('timed out')) {
rethrow;
throw DioException.connectionError(
requestOptions: options,
reason: e.message,
error: e,
);
}
throw DioException.connectionTimeout(
requestOptions: options,
Expand Down
12 changes: 6 additions & 6 deletions dio/lib/src/dio_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ class DioException implements Exception {
factory DioException.connectionError({
required RequestOptions requestOptions,
required String reason,
Object? error,
}) =>
DioException(
type: DioExceptionType.connectionError,
message: 'The connection errored: $reason',
requestOptions: requestOptions,
response: null,
error: null,
);
type: DioExceptionType.connectionError,
message: 'The connection errored: $reason',
requestOptions: requestOptions,
response: null,
error: error);

/// The request info for the request that throws exception.
final RequestOptions requestOptions;
Expand Down
2 changes: 1 addition & 1 deletion dio/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dio
version: 5.3.1
version: 5.3.2

description: |
A powerful HTTP networking package,
Expand Down
6 changes: 1 addition & 5 deletions dio/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ void main() {
throwsA(
allOf([
isA<DioException>(),
(DioException e) =>
e.type ==
(isWeb
? DioExceptionType.connectionError
: DioExceptionType.unknown),
(DioException e) => e.type == (DioExceptionType.connectionError),
if (!isWeb) (DioException e) => e.error is SocketException,
]),
),
Expand Down
53 changes: 26 additions & 27 deletions dio/test/stacktrace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,59 +205,58 @@ void main() async {
},
testOn: '!browser',
);

group('DioExceptionType.unknown', () {
group('DioExceptionType.connectionError', () {
test(
JsonUnsupportedObjectError,
'SocketException on request',
() async {
final dio = Dio()..options.baseUrl = 'https://does.not.exist';

final dio = Dio()
..options.baseUrl = 'https://does.not.exist'
..httpClientAdapter = IOHttpClientAdapter();
await expectLater(
dio.get(
'/test',
options: Options(contentType: Headers.jsonContentType),
data: Object(),
),
dio.get('/test', data: 'test'),
throwsA(
allOf([
isA<DioException>(),
(DioException e) => e.type == DioExceptionType.unknown,
(DioException e) => e.error is JsonUnsupportedObjectError,
(DioException e) => e.stackTrace
(e) => e.type == DioExceptionType.connectionError,
(e) => e.error is SocketException,
(e) => (e.error as SocketException)
.message
.contains("Failed host lookup: 'does.not.exist'"),
(e) => e.stackTrace
.toString()
.contains('test/stacktrace_test.dart'),
]),
),
);
},
testOn: '!browser',
testOn: 'vm',
);

});
group('DioExceptionType.unknown', () {
test(
'SocketException on request',
JsonUnsupportedObjectError,
() async {
final dio = Dio()
..options.baseUrl = 'https://does.not.exist'
..httpClientAdapter = IOHttpClientAdapter();
final dio = Dio()..options.baseUrl = 'https://does.not.exist';

await expectLater(
dio.get('/test', data: 'test'),
dio.get(
'/test',
options: Options(contentType: Headers.jsonContentType),
data: Object(),
),
throwsA(
allOf([
isA<DioException>(),
(e) => e.type == DioExceptionType.unknown,
(e) => e.error is SocketException,
(e) => (e.error as SocketException)
.message
.startsWith("Failed host lookup: 'does.not.exist'"),
(e) => e.stackTrace
(DioException e) => e.type == DioExceptionType.unknown,
(DioException e) => e.error is JsonUnsupportedObjectError,
(DioException e) => e.stackTrace
.toString()
.contains('test/stacktrace_test.dart'),
]),
),
);
},
testOn: 'vm',
testOn: '!browser',
);

test(
Expand Down
3 changes: 3 additions & 0 deletions plugins/native_dio_adapter/.flutter-plugins
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is a generated file; do not edit or check into version control.
cronet_http=/Users/AbdelazizMahdy/.pub-cache/hosted/pub.dev/cronet_http-0.2.1/
cupertino_http=/Users/AbdelazizMahdy/.pub-cache/hosted/pub.dev/cupertino_http-1.0.1/
1 change: 1 addition & 0 deletions plugins/native_dio_adapter/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"cupertino_http","path":"/Users/AbdelazizMahdy/.pub-cache/hosted/pub.dev/cupertino_http-1.0.1/","native_build":true,"dependencies":[]}],"android":[{"name":"cronet_http","path":"/Users/AbdelazizMahdy/.pub-cache/hosted/pub.dev/cronet_http-0.2.1/","native_build":true,"dependencies":[]}],"macos":[{"name":"cupertino_http","path":"/Users/AbdelazizMahdy/.pub-cache/hosted/pub.dev/cupertino_http-1.0.1/","native_build":true,"dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"cronet_http","dependencies":[]},{"name":"cupertino_http","dependencies":[]}],"date_created":"2023-07-31 23:02:58.574313","version":"3.10.6"}