OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 pub.exceptions; | |
6 | |
7 import 'dart:io'; | |
8 import 'dart:isolate'; | |
9 | |
10 import "package:analyzer/analyzer.dart"; | |
11 import 'package:args/command_runner.dart'; | |
12 import "package:http/http.dart" as http; | |
13 import "package:stack_trace/stack_trace.dart"; | |
14 import "package:yaml/yaml.dart"; | |
15 | |
16 import '../../asset/dart/serialize.dart'; | |
17 | |
18 /// An exception class for exceptions that are intended to be seen by the user. | |
19 /// | |
20 /// These exceptions won't have any debugging information printed when they're | |
21 /// thrown. | |
22 class ApplicationException implements Exception { | |
23 final String message; | |
24 | |
25 ApplicationException(this.message); | |
26 | |
27 String toString() => message; | |
28 } | |
29 | |
30 /// An exception class for exceptions that are intended to be seen by the user | |
31 /// and are associated with a problem in a file at some path. | |
32 class FileException implements ApplicationException { | |
33 final String message; | |
34 | |
35 /// The path to the file that was missing or erroneous. | |
36 final String path; | |
37 | |
38 FileException(this.message, this.path); | |
39 | |
40 String toString() => message; | |
41 } | |
42 | |
43 /// A class for exceptions that wrap other exceptions. | |
44 class WrappedException extends ApplicationException { | |
45 /// The underlying exception that [this] is wrapping, if any. | |
46 final innerError; | |
47 | |
48 /// The stack chain for [innerError] if it exists. | |
49 final Chain innerChain; | |
50 | |
51 WrappedException(String message, this.innerError, [StackTrace innerTrace]) | |
52 : innerChain = innerTrace == null ? null : new Chain.forTrace(innerTrace), | |
53 super(message); | |
54 } | |
55 | |
56 /// A class for exceptions that shouldn't be printed at the top level. | |
57 /// | |
58 /// This is usually used when an exception has already been printed using | |
59 /// [log.exception]. | |
60 class SilentException extends WrappedException { | |
61 SilentException(innerError, [StackTrace innerTrace]) | |
62 : super(innerError.toString(), innerError, innerTrace); | |
63 } | |
64 | |
65 /// A class for errors in a command's input data. | |
66 /// | |
67 /// This corresponds to the [exit_codes.DATA] exit code. | |
68 class DataException extends ApplicationException { | |
69 DataException(String message) | |
70 : super(message); | |
71 } | |
72 | |
73 /// An class for exceptions where a package could not be found in a [Source]. | |
74 /// | |
75 /// The source is responsible for wrapping its internal exceptions in this so | |
76 /// that other code in pub can use this to show a more detailed explanation of | |
77 /// why the package was being requested. | |
78 class PackageNotFoundException extends WrappedException { | |
79 PackageNotFoundException(String message, [innerError, StackTrace innerTrace]) | |
80 : super(message, innerError, innerTrace); | |
81 } | |
82 | |
83 /// All the names of user-facing exceptions. | |
84 final _userFacingExceptions = new Set<String>.from( | |
85 ['ApplicationException', 'GitException', // This refers to http.ClientExcept
ion. | |
86 'ClientException', | |
87 // Errors coming from the Dart analyzer are probably caused by syntax erro
rs | |
88 // in user code, so they're user-facing. | |
89 'AnalyzerError', | |
90 'AnalyzerErrorGroup', | |
91 // An error spawning an isolate probably indicates a transformer with an | |
92 // invalid import. | |
93 'IsolateSpawnException', // IOException and subclasses. | |
94 'CertificateException', | |
95 'FileSystemException', | |
96 'HandshakeException', | |
97 'HttpException', | |
98 'IOException', | |
99 'ProcessException', | |
100 'RedirectException', | |
101 'SignalException', | |
102 'SocketException', | |
103 'StdoutException', | |
104 'TlsException', | |
105 'WebSocketException']); | |
106 | |
107 /// Returns whether [error] is a user-facing error object. | |
108 /// | |
109 /// This includes both [ApplicationException] and any dart:io errors. | |
110 bool isUserFacingException(error) { | |
111 if (error is CrossIsolateException) { | |
112 return _userFacingExceptions.contains(error.type); | |
113 } | |
114 | |
115 // TODO(nweiz): unify this list with _userFacingExceptions when issue 5897 is | |
116 // fixed. | |
117 return error is ApplicationException || | |
118 error is AnalyzerError || | |
119 error is AnalyzerErrorGroup || | |
120 error is IsolateSpawnException || | |
121 error is IOException || | |
122 error is http.ClientException || | |
123 error is YamlException || | |
124 error is UsageException; | |
125 } | |
OLD | NEW |