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