Skip to content

Commit

Permalink
Add a better toString to _ClientSocketException (#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan authored May 25, 2023
1 parent 5c1f1ad commit 8a4a4a6
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.1

* Add better error messages for `SocketException`s when using `IOClient`.

## 1.0.0

* Requires Dart 3.0 or later.
Expand Down
4 changes: 4 additions & 0 deletions pkgs/http/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import 'streamed_response.dart';
/// [http.head], [http.get], [http.post], [http.put], [http.patch], or
/// [http.delete] instead.
///
/// All methods will emit a [ClientException] if there is a transport-level
/// failure when communication with the server. For example, if the server could
/// not be reached.
///
/// When creating an HTTP client class with additional functionality, you must
/// extend [BaseClient] rather than [Client]. In most cases, you can wrap
/// another instance of [Client] and add functionality on top of that. This
Expand Down
8 changes: 7 additions & 1 deletion pkgs/http/lib/src/exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ class ClientException implements Exception {
ClientException(this.message, [this.uri]);

@override
String toString() => message;
String toString() {
if (uri != null) {
return 'ClientException: $message, uri=$uri';
} else {
return 'ClientException: $message';
}
}
}
31 changes: 28 additions & 3 deletions pkgs/http/lib/src/io_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:io';

import 'base_client.dart';
import 'base_request.dart';
import 'client.dart';
import 'exception.dart';
import 'io_streamed_response.dart';

Expand All @@ -28,9 +29,9 @@ BaseClient createClient() {
class _ClientSocketException extends ClientException
implements SocketException {
final SocketException cause;
_ClientSocketException(SocketException e, Uri url)
_ClientSocketException(SocketException e, Uri uri)
: cause = e,
super(e.message, url);
super(e.message, uri);

@override
InternetAddress? get address => cause.address;
Expand All @@ -40,9 +41,33 @@ class _ClientSocketException extends ClientException

@override
int? get port => cause.port;

@override
String toString() => 'ClientException with $cause, uri=$uri';
}

/// A `dart:io`-based HTTP client.
/// A `dart:io`-based HTTP [Client].
///
/// If there is a socket-level failure when communicating with the server
/// (for example, if the server could not be reached), [IOClient] will emit a
/// [ClientException] that also implements [SocketException]. This allows
/// callers to get more detailed exception information for socket-level
/// failures, if desired.
///
/// For example:
/// ```dart
/// final client = http.Client();
/// late String data;
/// try {
/// data = await client.read(Uri.https('example.com', ''));
/// } on SocketException catch (e) {
/// // Exception is transport-related, check `e.osError` for more details.
/// } on http.ClientException catch (e) {
/// // Exception is HTTP-related (e.g. the server returned a 404 status code).
/// // If the handler for `SocketException` were removed then all exceptions
/// // would be caught by this handler.
/// }
/// ```
class IOClient extends BaseClient {
/// The underlying `dart:io` HTTP client.
HttpClient? _inner;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/http/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: http
version: 1.0.0
version: 1.0.1-wip
description: A composable, multi-platform, Future-based API for HTTP requests.
repository: https://github.com/dart-lang/http/tree/master/pkgs/http

Expand Down
10 changes: 9 additions & 1 deletion pkgs/http/test/io/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ void main() {
request.headers[HttpHeaders.contentTypeHeader] =
'application/json; charset=utf-8';

expect(client.send(request), throwsA(isA<SocketException>()));
expect(
client.send(request),
throwsA(allOf(
isA<http.ClientException>().having((e) => e.uri, 'uri', url),
isA<SocketException>().having(
(e) => e.toString(),
'SocketException.toString',
matches('ClientException with SocketException.*,'
' uri=http://http.invalid')))));

request.sink.add('{"hello": "world"}'.codeUnits);
request.sink.close();
Expand Down

0 comments on commit 8a4a4a6

Please sign in to comment.