Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Unified Diff: lib/src/io_io.dart

Issue 2638403002: Make failing HTTP requests thrown when using dart:io. (Closed)
Patch Set: Fix typo in changelog Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/io_io.dart
diff --git a/lib/src/io_io.dart b/lib/src/io_io.dart
index d4edfeacac6502491f9e8707899efe33275680dc..1056d755649dc00ae6797ac11470daf8a1fc30dd 100644
--- a/lib/src/io_io.dart
+++ b/lib/src/io_io.dart
@@ -4,8 +4,12 @@
import "dart:async" show Future, Stream;
import "dart:convert" show Encoding, LATIN1, UTF8;
-import "dart:io" show
- File, HttpClient, HttpClientResponse, HttpClientRequest, HttpHeaders;
+import "dart:io" show File,
+ HttpStatus,
+ HttpClient,
+ HttpClientResponse,
+ HttpClientRequest,
+ HttpHeaders;
import "package:typed_data/typed_buffers.dart" show Uint8Buffer;
@@ -17,6 +21,7 @@ Stream<List<int>> readAsStream(Uri uri) async* {
}
if (uri.scheme == "http" || uri.scheme == "https") {
HttpClientResponse response = await _httpGetBytes(uri);
+ _throwIfFailed(response, uri);
yield* response;
return;
}
@@ -34,6 +39,7 @@ Future<List<int>> readAsBytes(Uri uri) async {
}
if (uri.scheme == "http" || uri.scheme == "https") {
HttpClientResponse response = await _httpGetBytes(uri);
+ _throwIfFailed(response, uri);
int length = response.contentLength;
if (length < 0) length = 0;
var buffer = new Uint8Buffer(length);
@@ -62,6 +68,7 @@ Future<String> readAsString(Uri uri, Encoding encoding) async {
request.headers.set(HttpHeaders.ACCEPT_CHARSET, encoding.name);
}
HttpClientResponse response = await request.close();
+ _throwIfFailed(response, uri);
encoding ??= Encoding.getByName(response.headers.contentType?.charset);
if (encoding == null || encoding == LATIN1) {
// Default to LATIN-1 if no encoding found.
@@ -88,3 +95,10 @@ Future<HttpClientResponse> _httpGetBytes(Uri uri) async {
request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*");
return request.close();
}
+
+void _throwIfFailed(HttpClientResponse response, Uri uri) {
+ var statusCode = response.statusCode;
+ if (statusCode < HttpStatus.OK || statusCode > HttpStatus.NO_CONTENT) {
+ throw new HttpException(response.reasonPhrase, uri: uri);
+ }
+}
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698