Commit 7745dd73 authored by Dan Rubel's avatar Dan Rubel Committed by GitHub

improve flutter tool error message for download problems (#6238)

parent 1b9c1bd2
......@@ -8,6 +8,7 @@ import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:stack_trace/stack_trace.dart';
import 'src/base/common.dart';
import 'src/base/context.dart';
import 'src/base/logger.dart';
import 'src/base/process.dart';
......@@ -102,6 +103,16 @@ Future<Null> main(List<String> args) async {
);
// Argument error exit code.
_exit(64);
} else if (error is ToolExit) {
stderr.writeln(error.message);
if (verbose) {
stderr.writeln();
stderr.writeln(chain.terse.toString());
stderr.writeln();
}
stderr.writeln('If this problem persists, please report the problem at');
stderr.writeln('https://github.com/flutter/flutter/issues/new');
_exit(error.exitCode ?? 65);
} else if (error is ProcessExit) {
// We've caught an exit code.
_exit(error.exitCode);
......
......@@ -5,3 +5,17 @@
const int kDefaultObservatoryPort = 8100;
const int kDefaultDiagnosticPort = 8101;
const int kDefaultDrivePort = 8183;
/// Specialized exception for expected situations
/// where the tool should exit with a clear message to the user
/// and no stack trace unless the --verbose option is specified.
/// For example: network errors
class ToolExit implements Exception {
ToolExit(this.message, { this.exitCode });
final String message;
final int exitCode;
@override
String toString() => "Exception: $message";
}
......@@ -6,6 +6,9 @@ import 'dart:async';
import 'dart:io';
import '../globals.dart';
import 'common.dart';
const int kNetworkProblemExitCode = 50;
/// Download a file from the given URL and return the bytes.
Future<List<int>> fetchUrl(Uri url) async {
......@@ -16,8 +19,13 @@ Future<List<int>> fetchUrl(Uri url) async {
HttpClientResponse response = await request.close();
printTrace('Received response statusCode=${response.statusCode}');
if (response.statusCode != 200)
throw new Exception(response.reasonPhrase);
if (response.statusCode != 200) {
throw new ToolExit(
'Download failed: $url\n'
' because (${response.statusCode}) ${response.reasonPhrase}',
exitCode: kNetworkProblemExitCode,
);
}
BytesBuilder responseBody = new BytesBuilder(copy: false);
await for (List<int> chunk in response)
......
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