Move crash reporting to own file
Change-Id: I9031458aa60ba4ee5b4b1594912f64c35ff6b8be
Reviewed-on: https://dart-review.googlesource.com/63105
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Peter von der Ahé <ahe@google.com>
diff --git a/pkg/front_end/lib/src/fasta/command_line_reporting.dart b/pkg/front_end/lib/src/fasta/command_line_reporting.dart
index 3543c8f..28414b3 100644
--- a/pkg/front_end/lib/src/fasta/command_line_reporting.dart
+++ b/pkg/front_end/lib/src/fasta/command_line_reporting.dart
@@ -20,8 +20,9 @@
import 'compiler_context.dart' show CompilerContext;
-import 'deprecated_problems.dart'
- show Crash, deprecated_InputError, safeToString;
+import 'crash.dart' show Crash, safeToString;
+
+import 'deprecated_problems.dart' show deprecated_InputError;
import 'fasta_codes.dart' show LocatedMessage;
diff --git a/pkg/front_end/lib/src/fasta/crash.dart b/pkg/front_end/lib/src/fasta/crash.dart
new file mode 100644
index 0000000..916d502
--- /dev/null
+++ b/pkg/front_end/lib/src/fasta/crash.dart
@@ -0,0 +1,114 @@
+// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library fasta.crash;
+
+import 'dart:async' show Future;
+
+import 'dart:convert' show jsonEncode;
+
+import 'dart:io'
+ show ContentType, HttpClient, HttpClientRequest, SocketException, stderr;
+
+const String defaultServerAddress = "http://127.0.0.1:59410/";
+
+/// Tracks if there has been a crash reported through [reportCrash]. Should be
+/// reset between each compilation by calling [resetCrashReporting].
+bool hasCrashed = false;
+
+/// Tracks the first source URI that has been read and is used as a fall-back
+/// for [reportCrash]. Should be reset between each compilation by calling
+/// [resetCrashReporting].
+Uri firstSourceUri;
+
+class Crash {
+ final Uri uri;
+
+ final int charOffset;
+
+ final Object error;
+
+ final StackTrace trace;
+
+ Crash(this.uri, this.charOffset, this.error, this.trace);
+
+ String toString() {
+ return """
+Crash when compiling $uri,
+at character offset $charOffset:
+$error${trace == null ? '' : '\n$trace'}
+""";
+ }
+}
+
+void resetCrashReporting() {
+ firstSourceUri = null;
+ hasCrashed = false;
+}
+
+Future<T> reportCrash<T>(error, StackTrace trace,
+ [Uri uri, int charOffset]) async {
+ note(String note) async {
+ stderr.write(note);
+ await stderr.flush();
+ }
+
+ if (hasCrashed) return new Future<T>.error(error, trace);
+ if (error is Crash) {
+ trace = error.trace ?? trace;
+ uri = error.uri ?? uri;
+ charOffset = error.charOffset ?? charOffset;
+ error = error.error;
+ }
+ uri ??= firstSourceUri;
+ hasCrashed = true;
+ Map<String, dynamic> data = <String, dynamic>{};
+ data["type"] = "crash";
+ data["client"] = "package:fasta";
+ if (uri != null) data["uri"] = "$uri";
+ if (charOffset != null) data["offset"] = charOffset;
+ data["error"] = safeToString(error);
+ data["trace"] = "$trace";
+ String json = jsonEncode(data);
+ HttpClient client = new HttpClient();
+ try {
+ Uri serverUri = Uri.parse(defaultServerAddress);
+ HttpClientRequest request;
+ try {
+ request = await client.postUrl(serverUri);
+ } on SocketException {
+ // Assume the crash logger isn't running.
+ client.close(force: true);
+ return new Future<T>.error(
+ new Crash(uri, charOffset, error, trace), trace);
+ }
+ if (request != null) {
+ await note("\nSending crash report data");
+ request.persistentConnection = false;
+ request.bufferOutput = false;
+ String host = request?.connectionInfo?.remoteAddress?.host;
+ int port = request?.connectionInfo?.remotePort;
+ await note(" to $host:$port");
+ await request
+ ..headers.contentType = ContentType.JSON
+ ..write(json);
+ await request.close();
+ await note(".");
+ }
+ } catch (e, s) {
+ await note("\n${safeToString(e)}\n$s\n");
+ await note("\n\n\nFE::ERROR::$json\n\n\n");
+ }
+ client.close(force: true);
+ await note("\n");
+ return new Future<T>.error(error, trace);
+}
+
+String safeToString(Object object) {
+ try {
+ return "$object";
+ } catch (e) {
+ return "Error when converting ${object.runtimeType} to string.";
+ }
+}
diff --git a/pkg/front_end/lib/src/fasta/deprecated_problems.dart b/pkg/front_end/lib/src/fasta/deprecated_problems.dart
index e197801..b06452f 100644
--- a/pkg/front_end/lib/src/fasta/deprecated_problems.dart
+++ b/pkg/front_end/lib/src/fasta/deprecated_problems.dart
@@ -4,31 +4,15 @@
library fasta.errors;
-import 'dart:async' show Future;
-
-import 'dart:convert' show jsonEncode;
-
-import 'dart:io'
- show ContentType, HttpClient, HttpClientRequest, SocketException, stderr;
-
import 'command_line_reporting.dart' show shouldThrowOn;
+import 'crash.dart' show safeToString;
+
import 'messages.dart'
show LocatedMessage, noLength, isVerbose, templateUnspecified;
import 'severity.dart' show Severity;
-const String defaultServerAddress = "http://127.0.0.1:59410/";
-
-/// Tracks if there has been a crash reported through [reportCrash]. Should be
-/// reset between each compilation by calling [resetCrashReporting].
-bool hasCrashed = false;
-
-/// Tracks the first source URI that has been read and is used as a fall-back
-/// for [reportCrash]. Should be reset between each compilation by calling
-/// [resetCrashReporting].
-Uri firstSourceUri;
-
/// Used to report an error in input.
///
/// Avoid using this for reporting compile-time errors, instead use
@@ -68,94 +52,3 @@
.withLocation(error.uri, error.charOffset, noLength);
}
}
-
-class Crash {
- final Uri uri;
-
- final int charOffset;
-
- final Object error;
-
- final StackTrace trace;
-
- Crash(this.uri, this.charOffset, this.error, this.trace);
-
- String toString() {
- return """
-Crash when compiling $uri,
-at character offset $charOffset:
-$error${trace == null ? '' : '\n$trace'}
-""";
- }
-}
-
-void resetCrashReporting() {
- firstSourceUri = null;
- hasCrashed = false;
-}
-
-Future<T> reportCrash<T>(error, StackTrace trace,
- [Uri uri, int charOffset]) async {
- note(String note) async {
- stderr.write(note);
- await stderr.flush();
- }
-
- if (hasCrashed) return new Future<T>.error(error, trace);
- if (error is Crash) {
- trace = error.trace ?? trace;
- uri = error.uri ?? uri;
- charOffset = error.charOffset ?? charOffset;
- error = error.error;
- }
- uri ??= firstSourceUri;
- hasCrashed = true;
- Map<String, dynamic> data = <String, dynamic>{};
- data["type"] = "crash";
- data["client"] = "package:fasta";
- if (uri != null) data["uri"] = "$uri";
- if (charOffset != null) data["offset"] = charOffset;
- data["error"] = safeToString(error);
- data["trace"] = "$trace";
- String json = jsonEncode(data);
- HttpClient client = new HttpClient();
- try {
- Uri serverUri = Uri.parse(defaultServerAddress);
- HttpClientRequest request;
- try {
- request = await client.postUrl(serverUri);
- } on SocketException {
- // Assume the crash logger isn't running.
- client.close(force: true);
- return new Future<T>.error(
- new Crash(uri, charOffset, error, trace), trace);
- }
- if (request != null) {
- await note("\nSending crash report data");
- request.persistentConnection = false;
- request.bufferOutput = false;
- String host = request?.connectionInfo?.remoteAddress?.host;
- int port = request?.connectionInfo?.remotePort;
- await note(" to $host:$port");
- await request
- ..headers.contentType = ContentType.JSON
- ..write(json);
- await request.close();
- await note(".");
- }
- } catch (e, s) {
- await note("\n${safeToString(e)}\n$s\n");
- await note("\n\n\nFE::ERROR::$json\n\n\n");
- }
- client.close(force: true);
- await note("\n");
- return new Future<T>.error(error, trace);
-}
-
-String safeToString(Object object) {
- try {
- return "$object";
- } catch (e) {
- return "Error when converting ${object.runtimeType} to string.";
- }
-}
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
index 2608d35..94631cb 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
@@ -47,8 +47,9 @@
import '../compiler_context.dart' show CompilerContext;
-import '../deprecated_problems.dart'
- show deprecated_InputError, reportCrash, resetCrashReporting;
+import '../crash.dart' show reportCrash, resetCrashReporting;
+
+import '../deprecated_problems.dart' show deprecated_InputError;
import '../dill/dill_target.dart' show DillTarget;
diff --git a/pkg/front_end/lib/src/fasta/loader.dart b/pkg/front_end/lib/src/fasta/loader.dart
index b86ce27..9c6044c 100644
--- a/pkg/front_end/lib/src/fasta/loader.dart
+++ b/pkg/front_end/lib/src/fasta/loader.dart
@@ -10,7 +10,7 @@
import 'builder/builder.dart' show Declaration, LibraryBuilder;
-import 'deprecated_problems.dart' show firstSourceUri;
+import 'crash.dart' show firstSourceUri;
import 'messages.dart'
show
diff --git a/pkg/front_end/lib/src/fasta/source/diet_listener.dart b/pkg/front_end/lib/src/fasta/source/diet_listener.dart
index 722435f..edfe289 100644
--- a/pkg/front_end/lib/src/fasta/source/diet_listener.dart
+++ b/pkg/front_end/lib/src/fasta/source/diet_listener.dart
@@ -27,8 +27,10 @@
import '../constant_context.dart' show ConstantContext;
+import '../crash.dart' show Crash;
+
import '../deprecated_problems.dart'
- show Crash, deprecated_InputError, deprecated_inputError;
+ show deprecated_InputError, deprecated_inputError;
import '../fasta_codes.dart'
show Message, messageExpectedBlockToSkip, templateInternalProblemNotFound;
diff --git a/pkg/front_end/lib/src/kernel_generator_impl.dart b/pkg/front_end/lib/src/kernel_generator_impl.dart
index b699884..5b085d6 100644
--- a/pkg/front_end/lib/src/kernel_generator_impl.dart
+++ b/pkg/front_end/lib/src/kernel_generator_impl.dart
@@ -6,18 +6,27 @@
library front_end.kernel_generator_impl;
import 'dart:async' show Future;
-import 'dart:async';
import 'package:kernel/kernel.dart' show Component, CanonicalName;
-import 'base/processed_options.dart';
-import 'fasta/severity.dart' show Severity;
+import 'base/processed_options.dart' show ProcessedOptions;
+
import 'fasta/compiler_context.dart' show CompilerContext;
-import 'fasta/deprecated_problems.dart' show deprecated_InputError, reportCrash;
+
+import 'fasta/crash.dart' show reportCrash;
+
+import 'fasta/deprecated_problems.dart' show deprecated_InputError;
+
import 'fasta/dill/dill_target.dart' show DillTarget;
+
import 'fasta/kernel/kernel_target.dart' show KernelTarget;
-import 'fasta/kernel/utils.dart';
-import 'fasta/kernel/verifier.dart';
+
+import 'fasta/kernel/utils.dart' show printComponentText, serializeComponent;
+
+import 'fasta/kernel/verifier.dart' show verifyComponent;
+
+import 'fasta/severity.dart' show Severity;
+
import 'fasta/uri_translator.dart' show UriTranslator;
/// Implementation for the
diff --git a/pkg/front_end/tool/_fasta/log_collector.dart b/pkg/front_end/tool/_fasta/log_collector.dart
index 971cbfe..5f3e184 100644
--- a/pkg/front_end/tool/_fasta/log_collector.dart
+++ b/pkg/front_end/tool/_fasta/log_collector.dart
@@ -8,8 +8,7 @@
import 'dart:io';
-import 'package:front_end/src/fasta/deprecated_problems.dart'
- show defaultServerAddress;
+import 'package:front_end/src/fasta/crash.dart' show defaultServerAddress;
badRequest(HttpRequest request, int status, String message) {
request.response.statusCode = status;