OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library fasta.errors; | 5 library fasta.errors; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 | 8 |
9 import 'dart:convert' show JSON; | 9 import 'dart:convert' show JSON; |
10 | 10 |
11 import 'dart:io' | 11 import 'dart:io' |
12 show ContentType, HttpClient, HttpClientRequest, SocketException, stderr; | 12 show ContentType, HttpClient, HttpClientRequest, SocketException, stderr; |
13 | 13 |
14 import 'colors.dart' show red; | 14 import 'command_line_reporting.dart' show isFatal; |
15 | 15 |
16 import 'messages.dart' show errorsAreFatal, deprecated_format, isVerbose; | 16 import 'messages.dart' show LocatedMessage, isVerbose, templateUnspecified; |
| 17 |
| 18 import 'severity.dart' show Severity; |
17 | 19 |
18 const String defaultServerAddress = "http://127.0.0.1:59410/"; | 20 const String defaultServerAddress = "http://127.0.0.1:59410/"; |
19 | 21 |
20 /// Tracks if there has been a crash reported through [reportCrash]. Should be | 22 /// Tracks if there has been a crash reported through [reportCrash]. Should be |
21 /// reset between each compilation by calling [resetCrashReporting]. | 23 /// reset between each compilation by calling [resetCrashReporting]. |
22 bool hasCrashed = false; | 24 bool hasCrashed = false; |
23 | 25 |
24 /// Tracks the first source URI that has been read and is used as a fall-back | 26 /// Tracks the first source URI that has been read and is used as a fall-back |
25 /// for [reportCrash]. Should be reset between each compilation by calling | 27 /// for [reportCrash]. Should be reset between each compilation by calling |
26 /// [resetCrashReporting]. | 28 /// [resetCrashReporting]. |
27 Uri firstSourceUri; | 29 Uri firstSourceUri; |
28 | 30 |
29 /// Used to report an error in input. | 31 /// Used to report an error in input. |
30 /// | 32 /// |
31 /// Avoid using this for reporting compile-time errors, instead use | 33 /// Avoid using this for reporting compile-time errors, instead use |
32 /// `LibraryBuilder.addCompileTimeError` for those. | 34 /// `LibraryBuilder.addCompileTimeError` for those. |
33 /// | 35 /// |
34 /// An input error is any error that isn't an internal error. We use the term | 36 /// An input error is any error that isn't an internal error. We use the term |
35 /// "input error" in favor of "user error". This way, if an input error isn't | 37 /// "input error" in favor of "user error". This way, if an input error isn't |
36 /// handled correctly, the user will never see a stack trace that says "user | 38 /// handled correctly, the user will never see a stack trace that says "user |
37 /// error". | 39 /// error". |
38 dynamic deprecated_inputError(Uri uri, int charOffset, Object error) { | 40 dynamic deprecated_inputError(Uri uri, int charOffset, Object error) { |
39 if (errorsAreFatal && isVerbose) { | 41 if (isFatal(Severity.error) && isVerbose) { |
40 print(StackTrace.current); | 42 print(StackTrace.current); |
41 } | 43 } |
42 throw new deprecated_InputError(uri, charOffset, error); | 44 throw new deprecated_InputError(uri, charOffset, error); |
43 } | 45 } |
44 | 46 |
45 String deprecated_printUnexpected(Uri uri, int charOffset, String message) { | |
46 String formattedMessage = | |
47 deprecated_formatUnexpected(uri, charOffset, message); | |
48 if (errorsAreFatal) { | |
49 print(formattedMessage); | |
50 if (isVerbose) print(StackTrace.current); | |
51 throw new deprecated_InputError(uri, charOffset, message); | |
52 } | |
53 print(formattedMessage); | |
54 return formattedMessage; | |
55 } | |
56 | |
57 String deprecated_formatUnexpected(Uri uri, int charOffset, String message) { | |
58 return deprecated_format(uri, charOffset, colorError("Error: $message")); | |
59 } | |
60 | |
61 String colorError(String message) { | |
62 // TODO(ahe): Colors need to be optional. Doesn't work well in Emacs or on | |
63 // Windows. | |
64 return red(message); | |
65 } | |
66 | |
67 class deprecated_InputError { | 47 class deprecated_InputError { |
68 final Uri uri; | 48 final Uri uri; |
69 | 49 |
70 final int charOffset; | 50 final int charOffset; |
71 | 51 |
72 final Object error; | 52 final Object error; |
73 | 53 |
74 deprecated_InputError(this.uri, int charOffset, this.error) | 54 deprecated_InputError(this.uri, int charOffset, this.error) |
75 : this.charOffset = charOffset ?? -1; | 55 : this.charOffset = charOffset ?? -1; |
76 | 56 |
77 toString() => "deprecated_InputError: $error"; | 57 toString() => "deprecated_InputError: $error"; |
78 | 58 |
79 String deprecated_format() => | 59 /// Converts [error] to a [LocatedMessage] using [templateUnspecified]. Using |
80 deprecated_formatUnexpected(uri, charOffset, safeToString(error)); | 60 /// [templateUnspecified] is deprecated behavior. |
| 61 /// |
| 62 /// Static method to discourage use and requiring call-sites to include the |
| 63 /// text `deprecated_`. |
| 64 static LocatedMessage toMessage(deprecated_InputError error) { |
| 65 return templateUnspecified |
| 66 .withArguments(safeToString(error.error)) |
| 67 .withLocation(error.uri, error.charOffset); |
| 68 } |
81 } | 69 } |
82 | 70 |
83 class Crash { | 71 class Crash { |
84 final Uri uri; | 72 final Uri uri; |
85 | 73 |
86 final int charOffset; | 74 final int charOffset; |
87 | 75 |
88 final Object error; | 76 final Object error; |
89 | 77 |
90 final StackTrace trace; | 78 final StackTrace trace; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 return new Future.error(error, trace); | 149 return new Future.error(error, trace); |
162 } | 150 } |
163 | 151 |
164 String safeToString(Object object) { | 152 String safeToString(Object object) { |
165 try { | 153 try { |
166 return "$object"; | 154 return "$object"; |
167 } catch (e) { | 155 } catch (e) { |
168 return "Error when converting ${object.runtimeType} to string."; | 156 return "Error when converting ${object.runtimeType} to string."; |
169 } | 157 } |
170 } | 158 } |
OLD | NEW |