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