This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Improve DioError
s
#16
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e587ca
improve dio errors
ueman 28f1fd0
fix some stacktrace handling
ueman 275a984
improve changelog for dio errors
ueman 5aba8b6
Update dio/lib/src/dio_error.dart
ueman 7727460
Merge branch 'main' into improve-dio-errors
ueman 9c6af33
improve test and toString()
ueman 1f4ff87
apply suggestions
ueman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,19 +37,19 @@ class IOHttpClientAdapter implements HttpClientAdapter { | |
var _httpClient = _configHttpClient(cancelFuture, options.connectTimeout); | ||
var reqFuture = _httpClient.openUrl(options.method, options.uri); | ||
|
||
void _throwConnectingTimeout() { | ||
throw DioError( | ||
requestOptions: options, | ||
error: 'Connecting timed out [${options.connectTimeout}ms]', | ||
type: DioErrorType.connectTimeout, | ||
); | ||
} | ||
|
||
late HttpClientRequest request; | ||
try { | ||
final connectionTimeout = options.connectTimeout; | ||
if (connectionTimeout != null) { | ||
request = await reqFuture.timeout(connectionTimeout); | ||
request = await reqFuture.timeout( | ||
connectionTimeout, | ||
onTimeout: () { | ||
throw DioError.connectionTimeout( | ||
requestOptions: options, | ||
timeout: connectionTimeout, | ||
); | ||
}, | ||
); | ||
} else { | ||
request = await reqFuture; | ||
} | ||
|
@@ -58,13 +58,18 @@ class IOHttpClientAdapter implements HttpClientAdapter { | |
options.headers.forEach((k, v) { | ||
if (v != null) request.headers.set(k, '$v'); | ||
}); | ||
} on SocketException catch (e) { | ||
if (e.message.contains('timed out')) { | ||
_throwConnectingTimeout(); | ||
} on SocketException catch (e, stackTrace) { | ||
if (!e.message.contains('timed out')) { | ||
rethrow; | ||
} | ||
rethrow; | ||
} on TimeoutException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to remove this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the exceptions are now thrown in the
See https://api.dart.dev/stable/2.18.4/dart-async/Future/timeout.html |
||
_throwConnectingTimeout(); | ||
throw DioError.connectionTimeout( | ||
requestOptions: options, | ||
timeout: options.connectTimeout ?? | ||
_httpClient.connectionTimeout ?? | ||
Duration.zero, | ||
error: e, | ||
stackTrace: stackTrace, | ||
); | ||
} | ||
|
||
request.followRedirects = options.followRedirects; | ||
|
@@ -75,37 +80,38 @@ class IOHttpClientAdapter implements HttpClientAdapter { | |
var future = request.addStream(requestStream); | ||
final sendTimeout = options.sendTimeout; | ||
if (sendTimeout != null) { | ||
future = future.timeout(sendTimeout); | ||
} | ||
try { | ||
await future; | ||
} on TimeoutException { | ||
request.abort(); | ||
throw DioError( | ||
requestOptions: options, | ||
error: 'Sending timeout[${options.sendTimeout}ms]', | ||
type: DioErrorType.sendTimeout, | ||
future = future.timeout( | ||
sendTimeout, | ||
onTimeout: () { | ||
request.abort(); | ||
throw DioError.sendTimeout( | ||
timeout: sendTimeout, | ||
requestOptions: options, | ||
); | ||
}, | ||
); | ||
} | ||
|
||
await future; | ||
} | ||
|
||
final stopwatch = Stopwatch()..start(); | ||
var future = request.close(); | ||
final receiveTimeout = options.receiveTimeout; | ||
if (receiveTimeout != null) { | ||
future = future.timeout(receiveTimeout); | ||
} | ||
late HttpClientResponse responseStream; | ||
try { | ||
responseStream = await future; | ||
} on TimeoutException { | ||
throw DioError( | ||
requestOptions: options, | ||
error: 'Receiving data timeout[${options.receiveTimeout}]', | ||
type: DioErrorType.receiveTimeout, | ||
future = future.timeout( | ||
receiveTimeout, | ||
onTimeout: () { | ||
throw DioError.receiveTimeout( | ||
timeout: receiveTimeout, | ||
requestOptions: options, | ||
); | ||
}, | ||
); | ||
} | ||
|
||
final responseStream = await future; | ||
|
||
var stream = | ||
responseStream.transform<Uint8List>(StreamTransformer.fromHandlers( | ||
handleData: (data, sink) { | ||
|
@@ -114,10 +120,9 @@ class IOHttpClientAdapter implements HttpClientAdapter { | |
final receiveTimeout = options.receiveTimeout; | ||
if (receiveTimeout != null && duration > receiveTimeout) { | ||
sink.addError( | ||
DioError( | ||
DioError.receiveTimeout( | ||
timeout: receiveTimeout, | ||
requestOptions: options, | ||
error: 'Receiving data timeout[${options.receiveTimeout}]', | ||
type: DioErrorType.receiveTimeout, | ||
), | ||
); | ||
responseStream.detachSocket().then((socket) => socket.destroy()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking around the code again, should we return the closure here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might figure out a proper test for this, but no need to rush.