OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 library error; | |
5 | |
6 import 'dart:collection'; | |
7 | |
8 import 'generated/error.dart'; | |
9 | |
10 /// The maximum line length when printing extracted source code when converting | |
11 /// an [AnalyzerError] to a string. | |
12 final _MAX_ERROR_LINE_LENGTH = 120; | |
13 | |
14 /// A wrapper around [AnalysisError] that provides a more user-friendly string | |
15 /// representation. | |
16 class AnalyzerError implements Exception { | |
17 final AnalysisError error; | |
18 | |
19 AnalyzerError(this.error); | |
20 | |
21 String get message => toString(); | |
22 | |
23 String toString() { | |
24 var builder = new StringBuffer(); | |
25 | |
26 // Print a less friendly string representation to ensure that | |
27 // error.source.contents is not executed, as .contents it isn't async | |
28 builder.write("Error in ${error.source.shortName}: ${error.message}"); | |
29 | |
30 // var content = error.source.contents.data; | |
31 // var beforeError = content.substring(0, error.offset); | |
32 // var lineNumber = "\n".allMatches(beforeError).length + 1; | |
33 // builder.writeln("Error on line $lineNumber of ${error.source.fullName}: " | |
34 // "${error.message}"); | |
35 | |
36 // var errorLineIndex = beforeError.lastIndexOf("\n") + 1; | |
37 // var errorEndOfLineIndex = content.indexOf("\n", error.offset); | |
38 // if (errorEndOfLineIndex == -1) errorEndOfLineIndex = content.length; | |
39 // var errorLine = content.substring( | |
40 // errorLineIndex, errorEndOfLineIndex); | |
41 // var errorColumn = error.offset - errorLineIndex; | |
42 // var errorLength = error.length; | |
43 // | |
44 // // Ensure that the error line we display isn't too long. | |
45 // if (errorLine.length > _MAX_ERROR_LINE_LENGTH) { | |
46 // var leftLength = errorColumn; | |
47 // var rightLength = errorLine.length - leftLength; | |
48 // if (leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2 && | |
49 // rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { | |
50 // errorLine = "..." + errorLine.substring( | |
51 // errorColumn - _MAX_ERROR_LINE_LENGTH ~/ 2 + 3, | |
52 // errorColumn + _MAX_ERROR_LINE_LENGTH ~/ 2 - 3) | |
53 // + "..."; | |
54 // errorColumn = _MAX_ERROR_LINE_LENGTH ~/ 2; | |
55 // } else if (rightLength > _MAX_ERROR_LINE_LENGTH ~/ 2) { | |
56 // errorLine = errorLine.substring(0, _MAX_ERROR_LINE_LENGTH - 3) + "..."
; | |
57 // } else { | |
58 // assert(leftLength > _MAX_ERROR_LINE_LENGTH ~/ 2); | |
59 // errorColumn -= errorLine.length - _MAX_ERROR_LINE_LENGTH; | |
60 // errorLine = "..." + errorLine.substring( | |
61 // errorLine.length - _MAX_ERROR_LINE_LENGTH + 3, errorLine.length); | |
62 // } | |
63 // errorLength = math.min(errorLength, _MAX_ERROR_LINE_LENGTH - errorColumn
); | |
64 // } | |
65 // builder.writeln(errorLine); | |
66 // | |
67 // for (var i = 0; i < errorColumn; i++) builder.write(" "); | |
68 // for (var i = 0; i < errorLength; i++) builder.write("^"); | |
69 builder.writeln(); | |
70 | |
71 return builder.toString(); | |
72 } | |
73 } | |
74 | |
75 /// An error class that collects multiple [AnalyzerError]s that are emitted | |
76 /// during a single analysis. | |
77 class AnalyzerErrorGroup implements Exception { | |
78 final List<AnalyzerError> _errors; | |
79 AnalyzerErrorGroup(Iterable<AnalyzerError> errors) | |
80 : _errors = errors.toList(); | |
81 | |
82 /// Creates an [AnalyzerErrorGroup] from a list of lower-level | |
83 /// [AnalysisError]s. | |
84 AnalyzerErrorGroup.fromAnalysisErrors(Iterable<AnalysisError> errors) | |
85 : this(errors.map((e) => new AnalyzerError(e))); | |
86 | |
87 /// The errors in this collection. | |
88 List<AnalyzerError> get errors => | |
89 new UnmodifiableListView<AnalyzerError>(_errors); | |
90 | |
91 String get message => toString(); | |
92 String toString() => errors.join("\n"); | |
93 } | |
OLD | NEW |