OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 test.runner.load_exception; | 5 library test.runner.load_exception; |
6 | 6 |
7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
8 | 8 |
9 import 'package:path/path.dart' as p; | 9 import 'package:path/path.dart' as p; |
10 import 'package:source_span/source_span.dart'; | 10 import 'package:source_span/source_span.dart'; |
11 | 11 |
12 import '../utils.dart'; | 12 import '../utils.dart'; |
13 | 13 |
| 14 /// A regular expression for matching filename annotations in |
| 15 /// [IsolateSpawnException] messages. |
| 16 final _isolateFileRegExp = |
| 17 new RegExp(r"^'(file:/[^']+)': (error|warning): ", multiLine: true); |
| 18 |
14 class LoadException implements Exception { | 19 class LoadException implements Exception { |
15 final String path; | 20 final String path; |
16 | 21 |
17 final innerError; | 22 final innerError; |
18 | 23 |
19 LoadException(this.path, this.innerError); | 24 LoadException(this.path, this.innerError); |
20 | 25 |
21 String toString({bool color: false}) { | 26 String toString({bool color: false}) { |
22 var buffer = new StringBuffer(); | 27 var buffer = new StringBuffer(); |
23 if (color) buffer.write('\u001b[31m'); // red | 28 if (color) buffer.write('\u001b[31m'); // red |
24 buffer.write('Failed to load "$path":'); | 29 buffer.write('Failed to load "$path":'); |
25 if (color) buffer.write('\u001b[0m'); // no color | 30 if (color) buffer.write('\u001b[0m'); // no color |
26 | 31 |
27 var innerString = getErrorMessage(innerError); | 32 var innerString = getErrorMessage(innerError); |
28 if (innerError is IsolateSpawnException) { | 33 if (innerError is IsolateSpawnException) { |
29 // If this is a parse error, get rid of the noisy preamble. | 34 // If this is a parse error, clean up the noisy filename annotations. |
30 innerString = innerString | 35 innerString = innerString.replaceAllMapped(_isolateFileRegExp, (match) { |
31 .replaceFirst("'${p.toUri(p.absolute(path))}': error: ", ""); | 36 if (p.fromUri(match[1]) == p.absolute(path)) return ""; |
| 37 return "${p.prettyUri(match[1])}: "; |
| 38 }); |
32 | 39 |
33 // If this is a file system error, get rid of both the preamble and the | 40 // If this is a file system error, get rid of both the preamble and the |
34 // useless stack trace. | 41 // useless stack trace. |
35 | 42 |
36 // This message was used prior to 1.11.0-dev.3.0. | 43 // This message was used prior to 1.11.0-dev.3.0. |
37 innerString = innerString.replaceFirst( | 44 innerString = innerString.replaceFirst( |
38 "Unhandled exception:\n" | 45 "Unhandled exception:\n" |
39 "Uncaught Error: Load Error: ", | 46 "Uncaught Error: Load Error: ", |
40 ""); | 47 ""); |
41 | 48 |
42 // This message was used after 1.11.0-dev.3.0. | 49 // This message was used after 1.11.0-dev.3.0. |
43 innerString = innerString.replaceFirst( | 50 innerString = innerString.replaceFirst( |
44 "Unhandled exception:\n" | 51 "Unhandled exception:\n" |
45 "Load Error for ", | 52 "Load Error for ", |
46 ""); | 53 ""); |
47 | 54 |
48 innerString = innerString.replaceFirst("FileSystemException: ", ""); | 55 innerString = innerString.replaceFirst("FileSystemException: ", ""); |
49 innerString = innerString.split("Stack Trace:\n").first.trim(); | 56 innerString = innerString.split("Stack Trace:\n").first.trim(); |
50 } if (innerError is SourceSpanException) { | 57 } if (innerError is SourceSpanException) { |
51 innerString = innerError.toString(color: color) | 58 innerString = innerError.toString(color: color) |
52 .replaceFirst(" of $path", ""); | 59 .replaceFirst(" of $path", ""); |
53 } | 60 } |
54 | 61 |
55 buffer.write(innerString.contains("\n") ? "\n" : " "); | 62 buffer.write(innerString.contains("\n") ? "\n" : " "); |
56 buffer.write(innerString); | 63 buffer.write(innerString); |
57 return buffer.toString(); | 64 return buffer.toString(); |
58 } | 65 } |
59 } | 66 } |
OLD | NEW |