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