Chromium Code Reviews| 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 | 7 import 'dart:async' show |
| 8 Future; | 8 Future; |
| 9 | 9 |
| 10 import 'dart:convert' show | 10 import 'dart:convert' show |
| 11 JSON; | 11 JSON; |
| 12 | 12 |
| 13 import 'dart:io' show | 13 import 'dart:io' show |
| 14 ContentType, | 14 ContentType, |
| 15 HttpClient, | 15 HttpClient, |
| 16 HttpClientRequest, | 16 HttpClientRequest, |
| 17 SocketException, | 17 SocketException, |
| 18 stderr; | 18 stderr; |
| 19 | 19 |
| 20 import 'colors.dart' show | 20 import 'colors.dart' show |
| 21 red; | 21 red; |
| 22 | 22 |
| 23 import 'messages.dart' as messages; | 23 import 'messages.dart' show |
| 24 areErrorsFatal, | |
| 25 format, | |
| 26 isVerbose; | |
| 24 | 27 |
| 25 const String defaultServerAddress = "http://127.0.0.1:59410/"; | 28 const String defaultServerAddress = "http://127.0.0.1:59410/"; |
| 26 | 29 |
| 27 /// Tracks if there has been a crash reported through [reportCrash]. Should be | 30 /// Tracks if there has been a crash reported through [reportCrash]. Should be |
| 28 /// reset between each compilation by calling [resetCrashReporting]. | 31 /// reset between each compilation by calling [resetCrashReporting]. |
| 29 bool hasCrashed = false; | 32 bool hasCrashed = false; |
| 30 | 33 |
| 31 /// Tracks the first source URI that has been read and is used as a fall-back | 34 /// Tracks the first source URI that has been read and is used as a fall-back |
| 32 /// for [reportCrash]. Should be reset between each compilation by calling | 35 /// for [reportCrash]. Should be reset between each compilation by calling |
| 33 /// [resetCrashReporting]. | 36 /// [resetCrashReporting]. |
| 34 Uri firstSourceUri; | 37 Uri firstSourceUri; |
| 35 | 38 |
| 36 /// Used to report an internal error. | 39 /// Used to report an internal error. |
| 37 /// | 40 /// |
| 38 /// Internal errors should be avoided as best as possible, but are preferred | 41 /// Internal errors should be avoided as best as possible, but are preferred |
| 39 /// over assertion failures. Favor error messages that starts with "Internal | 42 /// over assertion failures. Favor error messages that starts with "Internal |
| 40 /// error: " and a short description that may help a developer debug the issue. | 43 /// error: " and a short description that may help a developer debug the issue. |
| 41 /// This method should be called instead of using `throw`, as this allows us to | 44 /// This method should be called instead of using `throw`, as this allows us to |
| 42 /// ensure that there are no throws anywhere in the codebase. | 45 /// ensure that there are no throws anywhere in the codebase. |
| 43 dynamic internalError(Object error, [Uri uri, int charOffset = -1]) { | 46 dynamic internalError(Object error, [Uri uri, int charOffset = -1]) { |
| 44 if (uri == null && charOffset == -1) { | 47 if (uri == null && charOffset == -1) { |
| 45 throw error; | 48 throw error; |
| 46 } else { | 49 } else { |
| 47 throw messages.format( | 50 throw format(uri, charOffset, "Internal error: ${safeToString(error)}"); |
| 48 uri, charOffset, "Internal error: ${safeToString(error)}"); | |
| 49 } | 51 } |
| 50 } | 52 } |
| 51 | 53 |
| 52 /// Used to report an error in input. | 54 /// Used to report an error in input. |
| 53 /// | 55 /// |
| 54 /// Avoid using this for reporting compile-time errors, instead use | 56 /// Avoid using this for reporting compile-time errors, instead use |
| 55 /// `LibraryBuilder.addCompileTimeError` for those. | 57 /// `LibraryBuilder.addCompileTimeError` for those. |
| 56 /// | 58 /// |
| 57 /// An input error is any error that isn't an internal error. We use the term | 59 /// An input error is any error that isn't an internal error. We use the term |
| 58 /// "input error" in favor of "user error". This way, if an input error isn't | 60 /// "input error" in favor of "user error". This way, if an input error isn't |
| 59 /// handled correctly, the user will never see a stack trace that says "user | 61 /// handled correctly, the user will never see a stack trace that says "user |
| 60 /// error". | 62 /// error". |
| 61 dynamic inputError(Uri uri, int charOffset, Object error) { | 63 dynamic inputError(Uri uri, int charOffset, Object error) { |
| 62 throw new InputError(uri, charOffset, error); | 64 throw new InputError(uri, charOffset, error); |
| 63 } | 65 } |
| 64 | 66 |
| 67 String printUnexpected(Uri uri, int charOffset, String message) { | |
| 68 if (areErrorsFatal) { | |
|
karlklose
2017/02/20 12:53:10
I would prefer to call these xxxAreFatal, because
ahe
2017/02/20 13:22:34
Done.
| |
| 69 if (isVerbose) print(StackTrace.current); | |
| 70 throw new InputError(uri, charOffset, message); | |
| 71 } | |
| 72 message = formatUnexpected(uri, charOffset, message); | |
| 73 print(message); | |
| 74 return message; | |
| 75 } | |
| 76 | |
| 77 String formatUnexpected(Uri uri, int charOffset, String message) { | |
| 78 return format(uri, charOffset, colorError("Error: $message")); | |
| 79 } | |
| 80 | |
| 65 String colorError(String message) { | 81 String colorError(String message) { |
| 66 // TODO(ahe): Colors need to be optional. Doesn't work well in Emacs or on | 82 // TODO(ahe): Colors need to be optional. Doesn't work well in Emacs or on |
| 67 // Windows. | 83 // Windows. |
| 68 return red(message); | 84 return red(message); |
| 69 } | 85 } |
| 70 | 86 |
| 71 class InputError { | 87 class InputError { |
| 72 final Uri uri; | 88 final Uri uri; |
| 73 | 89 |
| 74 final int charOffset; | 90 final int charOffset; |
| 75 | 91 |
| 76 final Object error; | 92 final Object error; |
| 77 | 93 |
| 78 InputError(this.uri, int charOffset, this.error) | 94 InputError(this.uri, int charOffset, this.error) |
| 79 : this.charOffset = charOffset ?? -1; | 95 : this.charOffset = charOffset ?? -1; |
| 80 | 96 |
| 81 toString() => "InputError: $error"; | 97 toString() => "InputError: $error"; |
| 82 | 98 |
| 83 String format() { | 99 String format() => formatUnexpected(uri, charOffset, safeToString(error)); |
| 84 return messages.format( | |
| 85 uri, charOffset, colorError("Error: ${safeToString(error)}")); | |
| 86 } | |
| 87 } | 100 } |
| 88 | 101 |
| 89 class Crash { | 102 class Crash { |
| 90 final Uri uri; | 103 final Uri uri; |
| 91 | 104 |
| 92 final int charOffset; | 105 final int charOffset; |
| 93 | 106 |
| 94 final Object error; | 107 final Object error; |
| 95 | 108 |
| 96 final StackTrace trace; | 109 final StackTrace trace; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 return new Future.error(error, trace); | 179 return new Future.error(error, trace); |
| 167 } | 180 } |
| 168 | 181 |
| 169 String safeToString(Object object) { | 182 String safeToString(Object object) { |
| 170 try { | 183 try { |
| 171 return "$object"; | 184 return "$object"; |
| 172 } catch (e) { | 185 } catch (e) { |
| 173 return "Error when converting ${object.runtimeType} to string."; | 186 return "Error when converting ${object.runtimeType} to string."; |
| 174 } | 187 } |
| 175 } | 188 } |
| OLD | NEW |