Commit da48e2a3 authored by Ian Hickson's avatar Ian Hickson

Clean up prints in services/*

We really shouldn't have two separate network library wrappers... I filed https://github.com/flutter/flutter/issues/2889 on that. But for now, this makes one of them have a generally better error handling story, and makes the other one only dump to the console in debug mode. It also makes both use debugPrint instead of print, and makes one use the 'mojom' prefix for mojom imports.
parent cef24293
...@@ -149,8 +149,14 @@ class MojoClient { ...@@ -149,8 +149,14 @@ class MojoClient {
ByteData data = await mojo.DataPipeDrainer.drainHandle(response.body); ByteData data = await mojo.DataPipeDrainer.drainHandle(response.body);
Uint8List bodyBytes = new Uint8List.view(data.buffer); Uint8List bodyBytes = new Uint8List.view(data.buffer);
return new Response(bodyBytes: bodyBytes, statusCode: response.statusCode); return new Response(bodyBytes: bodyBytes, statusCode: response.statusCode);
} catch (e) { } catch (exception) {
print("NetworkService unavailable $e"); assert(() {
debugPrint('-- EXCEPTION CAUGHT BY NETWORKING HTTP LIBRARY -------------------------');
debugPrint('An exception was raised while sending bytes to the Mojo network library:');
debugPrint('$exception');
debugPrint('------------------------------------------------------------------------');
return true;
});
return new Response(statusCode: 500); return new Response(statusCode: 500);
} finally { } finally {
loader.close(); loader.close();
......
...@@ -4,32 +4,47 @@ ...@@ -4,32 +4,47 @@
import 'dart:async'; import 'dart:async';
import 'package:mojo/mojo/url_request.mojom.dart'; import 'package:mojo/mojo/url_request.mojom.dart' as mojom;
import 'package:mojo/mojo/url_response.mojom.dart'; import 'package:mojo/mojo/url_response.mojom.dart' as mojom;
import 'package:mojo_services/mojo/url_loader.mojom.dart'; import 'package:mojo_services/mojo/url_loader.mojom.dart' as mojom;
import '../http/mojo_client.dart'; import '../http/mojo_client.dart';
import 'print.dart';
export 'package:mojo/mojo/url_response.mojom.dart' show UrlResponse; export 'package:mojo/mojo/url_response.mojom.dart' show UrlResponse;
Future<UrlResponse> fetch(UrlRequest request) async { Future<mojom.UrlResponse> fetch(mojom.UrlRequest request, { bool require200: false }) async {
UrlLoaderProxy loader = new UrlLoaderProxy.unbound(); mojom.UrlLoaderProxy loader = new mojom.UrlLoaderProxy.unbound();
try { try {
MojoClient.networkService.ptr.createUrlLoader(loader); MojoClient.networkService.ptr.createUrlLoader(loader);
UrlResponse response = (await loader.ptr.start(request)).response; mojom.UrlResponse response = (await loader.ptr.start(request)).response;
if (require200 && (response.error != null || response.statusCode != 200)) {
StringBuffer message = new StringBuffer();
message.writeln('Could not ${request.method ?? "fetch"} ${request.url ?? "resource"}');
if (response.error != null)
message.writeln('Network error: ${response.error.code} ${response.error.description ?? "<unknown network error>"}');
if (response.statusCode != 200)
message.writeln('Protocol error: ${response.statusCode} ${response.statusLine ?? "<no server message>"}');
if (response.url != request.url)
message.writeln('Final URL after redirects was: ${response.url}');
throw message;
}
return response; return response;
} catch (e) { } catch (exception) {
print("NetworkService unavailable $e"); debugPrint('-- EXCEPTION CAUGHT BY NETWORKING HTTP LIBRARY -------------------------');
return new UrlResponse()..statusCode = 500; debugPrint('An exception was raised while sending bytes to the Mojo network library:');
debugPrint('$exception');
debugPrint('------------------------------------------------------------------------');
return null;
} finally { } finally {
loader.close(); loader.close();
} }
} }
Future<UrlResponse> fetchUrl(String relativeUrl) { Future<mojom.UrlResponse> fetchUrl(String relativeUrl, { bool require200: false }) {
String url = Uri.base.resolve(relativeUrl).toString(); String url = Uri.base.resolve(relativeUrl).toString();
UrlRequest request = new UrlRequest() mojom.UrlRequest request = new mojom.UrlRequest()
..url = url ..url = url
..autoFollowRedirects = true; ..autoFollowRedirects = true;
return fetch(request); return fetch(request, require200: require200);
} }
...@@ -52,15 +52,14 @@ class _UrlFetcher implements ImageProvider { ...@@ -52,15 +52,14 @@ class _UrlFetcher implements ImageProvider {
@override @override
Future<ImageInfo> loadImage() async { Future<ImageInfo> loadImage() async {
UrlResponse response = await fetchUrl(_url); UrlResponse response = await fetchUrl(_url, require200: true);
if (response.statusCode >= 400) { if (response != null) {
print("Failed (${response.statusCode}) to load image $_url"); return new ImageInfo(
return null; image: await decodeImageFromDataPipe(response.body),
scale: _scale
);
} }
return new ImageInfo( return null;
image: await decodeImageFromDataPipe(response.body),
scale: _scale
);
} }
@override @override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment