| 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 'util/relativize.dart' show | 23 import 'messages.dart' as messages; |
| 24 relativizeUri; | |
| 25 | 24 |
| 26 const String defaultServerAddress = "http://127.0.0.1:59410/"; | 25 const String defaultServerAddress = "http://127.0.0.1:59410/"; |
| 27 | 26 |
| 28 /// Tracks if there has been a crash reported through [reportCrash]. Should be | 27 /// Tracks if there has been a crash reported through [reportCrash]. Should be |
| 29 /// reset between each compilation by calling [resetCrashReporting]. | 28 /// reset between each compilation by calling [resetCrashReporting]. |
| 30 bool hasCrashed = false; | 29 bool hasCrashed = false; |
| 31 | 30 |
| 32 /// Tracks the first source URI that has been read and is used as a fall-back | 31 /// Tracks the first source URI that has been read and is used as a fall-back |
| 33 /// for [reportCrash]. Should be reset between each compilation by calling | 32 /// for [reportCrash]. Should be reset between each compilation by calling |
| 34 /// [resetCrashReporting]. | 33 /// [resetCrashReporting]. |
| 35 Uri firstSourceUri; | 34 Uri firstSourceUri; |
| 36 | 35 |
| 37 /// Used to report an internal error. | 36 /// Used to report an internal error. |
| 38 /// | 37 /// |
| 39 /// Internal errors should be avoided as best as possible, but are preferred | 38 /// Internal errors should be avoided as best as possible, but are preferred |
| 40 /// over assertion failures. Favor error messages that starts with "Internal | 39 /// over assertion failures. Favor error messages that starts with "Internal |
| 41 /// error: " and a short description that may help a developer debug the issue. | 40 /// error: " and a short description that may help a developer debug the issue. |
| 42 /// This method should be called instead of using `throw`, as this allows us to | 41 /// This method should be called instead of using `throw`, as this allows us to |
| 43 /// ensure that there are no throws anywhere in the codebase. | 42 /// ensure that there are no throws anywhere in the codebase. |
| 44 dynamic internalError(Object error) { | 43 dynamic internalError(Object error, [Uri uri, int charOffset = -1]) { |
| 45 throw error; | 44 if (uri == null && charOffset == -1) { |
| 45 throw error; |
| 46 } else { |
| 47 throw messages.format( |
| 48 uri, charOffset, "Internal error: ${safeToString(error)}"); |
| 49 } |
| 46 } | 50 } |
| 47 | 51 |
| 48 /// Used to report an error in input. | 52 /// Used to report an error in input. |
| 49 /// | 53 /// |
| 50 /// Avoid using this for reporting compile-time errors, instead use | 54 /// Avoid using this for reporting compile-time errors, instead use |
| 51 /// `LibraryBuilder.addCompileTimeError` for those. | 55 /// `LibraryBuilder.addCompileTimeError` for those. |
| 52 /// | 56 /// |
| 53 /// An input error is any error that isn't an internal error. We use the term | 57 /// An input error is any error that isn't an internal error. We use the term |
| 54 /// "input error" in favor of "user error". This way, if an input error isn't | 58 /// "input error" in favor of "user error". This way, if an input error isn't |
| 55 /// handled correctly, the user will never see a stack trace that says "user | 59 /// handled correctly, the user will never see a stack trace that says "user |
| 56 /// error". | 60 /// error". |
| 57 dynamic inputError(Uri uri, int charOffset, Object error) { | 61 dynamic inputError(Uri uri, int charOffset, Object error) { |
| 58 throw new InputError(uri, charOffset, error); | 62 throw new InputError(uri, charOffset, error); |
| 59 } | 63 } |
| 60 | 64 |
| 65 String colorError(String message) { |
| 66 // TODO(ahe): Colors need to be optional. Doesn't work well in Emacs or on |
| 67 // Windows. |
| 68 return red(message); |
| 69 } |
| 70 |
| 61 class InputError { | 71 class InputError { |
| 62 final Uri uri; | 72 final Uri uri; |
| 63 | 73 |
| 64 final int charOffset; | 74 final int charOffset; |
| 65 | 75 |
| 66 final Object error; | 76 final Object error; |
| 67 | 77 |
| 68 InputError(this.uri, int charOffset, this.error) | 78 InputError(this.uri, int charOffset, this.error) |
| 69 : this.charOffset = charOffset ?? -1; | 79 : this.charOffset = charOffset ?? -1; |
| 70 | 80 |
| 71 toString() => "InputError: $error"; | 81 toString() => "InputError: $error"; |
| 72 | 82 |
| 73 String format() { | 83 String format() { |
| 74 // TODO(ahe): Colors need to be optional. Doesn't work well in Emacs or on | 84 return messages.format( |
| 75 // Windows. | 85 uri, charOffset, colorError("Error: ${safeToString(error)}")); |
| 76 String message = red("Error: ${safeToString(error)}"); | |
| 77 if (uri != null) { | |
| 78 String position = charOffset == -1 ? "" : "$charOffset:"; | |
| 79 return "${relativizeUri(uri)}:$position $message"; | |
| 80 } else { | |
| 81 return message; | |
| 82 } | |
| 83 } | 86 } |
| 84 } | 87 } |
| 85 | 88 |
| 86 class Crash { | 89 class Crash { |
| 87 final Uri uri; | 90 final Uri uri; |
| 88 | 91 |
| 89 final int charOffset; | 92 final int charOffset; |
| 90 | 93 |
| 91 final Object error; | 94 final Object error; |
| 92 | 95 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 return new Future.error(error, trace); | 166 return new Future.error(error, trace); |
| 164 } | 167 } |
| 165 | 168 |
| 166 String safeToString(Object object) { | 169 String safeToString(Object object) { |
| 167 try { | 170 try { |
| 168 return "$object"; | 171 return "$object"; |
| 169 } catch (e) { | 172 } catch (e) { |
| 170 return "Error when converting ${object.runtimeType} to string."; | 173 return "Error when converting ${object.runtimeType} to string."; |
| 171 } | 174 } |
| 172 } | 175 } |
| OLD | NEW |