Skip to content

Commit

Permalink
Fixing connection error (cfug#1912)
Browse files Browse the repository at this point in the history
<!-- Write down your pull request descriptions. -->

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

fixing cfug#1911

<!-- Provide more context and info about the PR. -->
  • Loading branch information
abdelaziz-mahdy authored Aug 8, 2023
1 parent 3db6af5 commit 0854519
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 45 deletions.
2 changes: 1 addition & 1 deletion dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ See the [Migration Guide][] for the complete breaking changes list.**

## Unreleased

*None.*
- Fix failing requests throw `DioException`s with `.unknown` instead of `.connectionError` on `SocketException`.

## 5.3.2

Expand Down
16 changes: 10 additions & 6 deletions dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,18 @@ class IOHttpClientAdapter implements HttpClientAdapter {
if (v != null) request.headers.set(k, v);
});
} on SocketException catch (e) {
if (!e.message.contains('timed out')) {
rethrow;
if (e.message.contains('timed out')) {
throw DioException.connectionTimeout(
requestOptions: options,
timeout: options.connectTimeout ??
httpClient.connectionTimeout ??
Duration.zero,
error: e,
);
}
throw DioException.connectionTimeout(
throw DioException.connectionError(
requestOptions: options,
timeout: options.connectTimeout ??
httpClient.connectionTimeout ??
Duration.zero,
reason: e.message,
error: e,
);
}
Expand Down
3 changes: 2 additions & 1 deletion dio/lib/src/dio_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +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,
error: error,
);

/// The request info for the request that throws exception.
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
58 changes: 26 additions & 32 deletions dio/test/stacktrace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,64 +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(
createHttpClient: () {
final client = MockHttpClient();
when(client.openUrl(any, any)).thenThrow(
SocketException('test'),
);
return client;
},
);
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.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

0 comments on commit 0854519

Please sign in to comment.