Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: sdk/lib/core/errors.dart

Issue 19579002: Revert "Introduce StackTraceOnThrowMixin." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/_internal/lib/js_helper.dart ('k') | sdk/lib/json/json.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of dart.core; 5 part of dart.core;
6 6
7 class Error { 7 class Error {
8 const Error();
9
10 /** 8 /**
11 * Safely convert a value to a [String] description. 9 * Safely convert a value to a [String] description.
12 * 10 *
13 * The conversion is guaranteed to not throw, so it won't use the object's 11 * The conversion is guaranteed to not throw, so it won't use the object's
14 * toString method. 12 * toString method.
15 */ 13 */
16 static String safeToString(Object object) { 14 static String safeToString(Object object) {
17 if (object is int || object is double || object is bool || null == object) { 15 if (object is int || object is double || object is bool || null == object) {
18 return object.toString(); 16 return object.toString();
19 } 17 }
20 if (object is String) { 18 if (object is String) {
21 // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed. 19 // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed.
22 String string = object; 20 String string = object;
23 const backslash = '\\'; 21 const backslash = '\\';
24 String escaped = string 22 String escaped = string
25 .replaceAll('$backslash', '$backslash$backslash') 23 .replaceAll('$backslash', '$backslash$backslash')
26 .replaceAll('\n', '${backslash}n') 24 .replaceAll('\n', '${backslash}n')
27 .replaceAll('\r', '${backslash}r') 25 .replaceAll('\r', '${backslash}r')
28 .replaceAll('"', '$backslash"'); 26 .replaceAll('"', '$backslash"');
29 return '"$escaped"'; 27 return '"$escaped"';
30 } 28 }
31 return _objectToString(object); 29 return _objectToString(object);
32 } 30 }
33 31
34 external static String _objectToString(Object object); 32 external static String _objectToString(Object object);
35 33
36 /**
37 * The stack trace of `this` error. May be null.
38 */
39 StackTrace get stackTrace => null;
40 }
41
42 /**
43 * Captures the stack trace when thrown.
44 */
45 class StackTraceOnThrow {
46 /**
47 * Captures the current stack-trace when thrown.
48 *
49 * Classes extending or mixing-in this class will automatically have a stack
50 * trace captured the first time they are thrown. This stack trace is returned
51 * by the [stackTrace] getter.
52 *
53 * Throwing the object again will not cause overwrite the first stack trace,
54 * so bjects that are `StackTraceOnThrow` instances should not be reused.
55 */
56 external StackTrace get stackTrace; 34 external StackTrace get stackTrace;
57 } 35 }
58 36
59 /** 37 /**
60 * Error thrown by the runtime system when an assert statement fails. 38 * Error thrown by the runtime system when an assert statement fails.
61 */ 39 */
62 class AssertionError extends Error with StackTraceOnThrow { 40 class AssertionError extends Error {
63 } 41 }
64 42
65 /** 43 /**
66 * Error thrown by the runtime system when a type assertion fails. 44 * Error thrown by the runtime system when a type assertion fails.
67 */ 45 */
68 class TypeError extends AssertionError { 46 class TypeError extends AssertionError {
69 } 47 }
70 48
71 /** 49 /**
72 * Error thrown by the runtime system when a cast operation fails. 50 * Error thrown by the runtime system when a cast operation fails.
73 */ 51 */
74 class CastError extends Error with StackTraceOnThrow { 52 class CastError extends Error {
75 } 53 }
76 54
77 /** 55 /**
78 * Error thrown when attempting to throw [:null:]. 56 * Error thrown when attempting to throw [:null:].
79 */ 57 */
80 class NullThrownError extends Error with StackTraceOnThrow { 58 class NullThrownError extends Error {
81 NullThrownError(); 59 NullThrownError();
82 String toString() => "Throw of null."; 60 String toString() => "Throw of null.";
83 } 61 }
84 62
85 /** 63 /**
86 * Error thrown when a function is passed an unacceptable argument. 64 * Error thrown when a function is passed an unacceptable argument.
87 */ 65 */
88 class ArgumentError extends Error with StackTraceOnThrow { 66 class ArgumentError extends Error {
89 final message; 67 final message;
90 68
91 /** The [message] describes the erroneous argument. */ 69 /** The [message] describes the erroneous argument. */
92 ArgumentError([this.message]); 70 ArgumentError([this.message]);
93 71
94 String toString() { 72 String toString() {
95 if (message != null) { 73 if (message != null) {
96 return "Illegal argument(s): $message"; 74 return "Illegal argument(s): $message";
97 } 75 }
98 return "Illegal argument(s)"; 76 return "Illegal argument(s)";
(...skipping 26 matching lines...) Expand all
125 103
126 104
127 /** 105 /**
128 * Error thrown when control reaches the end of a switch case. 106 * Error thrown when control reaches the end of a switch case.
129 * 107 *
130 * The Dart specification requires this error to be thrown when 108 * The Dart specification requires this error to be thrown when
131 * control reaches the end of a switch case (except the last case 109 * control reaches the end of a switch case (except the last case
132 * of a switch) without meeting a break or similar end of the control 110 * of a switch) without meeting a break or similar end of the control
133 * flow. 111 * flow.
134 */ 112 */
135 class FallThroughError extends Error with StackTraceOnThrow { 113 class FallThroughError extends Error {
114 FallThroughError();
136 } 115 }
137 116
138 117
139 class AbstractClassInstantiationError 118 class AbstractClassInstantiationError extends Error {
140 extends Error with StackTraceOnThrow {
141 final String _className; 119 final String _className;
142 AbstractClassInstantiationError(String this._className); 120 AbstractClassInstantiationError(String this._className);
143 String toString() => "Cannot instantiate abstract class: '$_className'"; 121 String toString() => "Cannot instantiate abstract class: '$_className'";
144 } 122 }
145 123
146 /** 124 /**
147 * Error thrown by the default implementation of [:noSuchMethod:] on [Object]. 125 * Error thrown by the default implementation of [:noSuchMethod:] on [Object].
148 */ 126 */
149 class NoSuchMethodError extends Error with StackTraceOnThrow { 127 class NoSuchMethodError extends Error {
150 final Object _receiver; 128 final Object _receiver;
151 final String _memberName; 129 final String _memberName;
152 final List _arguments; 130 final List _arguments;
153 final Map<String,dynamic> _namedArguments; 131 final Map<String,dynamic> _namedArguments;
154 final List _existingArgumentNames; 132 final List _existingArgumentNames;
155 133
156 /** 134 /**
157 * Create a [NoSuchMethodError] corresponding to a failed method call. 135 * Create a [NoSuchMethodError] corresponding to a failed method call.
158 * 136 *
159 * The first parameter to this constructor is the receiver of the method call. 137 * The first parameter to this constructor is the receiver of the method call.
(...skipping 17 matching lines...) Expand all
177 external String toString(); 155 external String toString();
178 } 156 }
179 157
180 158
181 /** 159 /**
182 * The operation was not allowed by the object. 160 * The operation was not allowed by the object.
183 * 161 *
184 * This [Error] is thrown when an instance cannot implement one of the methods 162 * This [Error] is thrown when an instance cannot implement one of the methods
185 * in its signature. 163 * in its signature.
186 */ 164 */
187 class UnsupportedError extends Error with StackTraceOnThrow { 165 class UnsupportedError extends Error {
188 final String message; 166 final String message;
189 UnsupportedError(this.message); 167 UnsupportedError(this.message);
190 String toString() => "Unsupported operation: $message"; 168 String toString() => "Unsupported operation: $message";
191 } 169 }
192 170
193 171
194 /** 172 /**
195 * Thrown by operations that have not been implemented yet. 173 * Thrown by operations that have not been implemented yet.
196 * 174 *
197 * This [Error] is thrown by unfinished code that hasn't yet implemented 175 * This [Error] is thrown by unfinished code that hasn't yet implemented
198 * all the features it needs. 176 * all the features it needs.
199 * 177 *
200 * If a class is not intending to implement the feature, it should throw 178 * If a class is not intending to implement the feature, it should throw
201 * an [UnsupportedError] instead. This error is only intended for 179 * an [UnsupportedError] instead. This error is only intended for
202 * use during development. 180 * use during development.
203 */ 181 */
204 class UnimplementedError 182 class UnimplementedError extends Error implements UnsupportedError {
205 extends Error with StackTraceOnThrow implements UnsupportedError {
206 final String message; 183 final String message;
207 UnimplementedError([String this.message]); 184 UnimplementedError([String this.message]);
208 String toString() => (this.message != null 185 String toString() => (this.message != null
209 ? "UnimplementedError: $message" 186 ? "UnimplementedError: $message"
210 : "UnimplementedError"); 187 : "UnimplementedError");
211 } 188 }
212 189
213 190
214 /** 191 /**
215 * The operation was not allowed by the current state of the object. 192 * The operation was not allowed by the current state of the object.
216 * 193 *
217 * This is a generic error used for a variety of different erroneous 194 * This is a generic error used for a variety of different erroneous
218 * actions. The message should be descriptive. 195 * actions. The message should be descriptive.
219 */ 196 */
220 class StateError extends Error with StackTraceOnThrow { 197 class StateError extends Error {
221 final String message; 198 final String message;
222 StateError(this.message); 199 StateError(this.message);
223 String toString() => "Bad state: $message"; 200 String toString() => "Bad state: $message";
224 } 201 }
225 202
226 203
227 /** 204 /**
228 * Error occurring when a collection is modified during iteration. 205 * Error occurring when a collection is modified during iteration.
229 * 206 *
230 * Some modifications may be allowed for some collections, so each collection 207 * Some modifications may be allowed for some collections, so each collection
231 * ([Iterable] or similar collection of values) should declare which operations 208 * ([Iterable] or similar collection of values) should declare which operations
232 * are allowed during an iteration. 209 * are allowed during an iteration.
233 */ 210 */
234 class ConcurrentModificationError extends Error with StackTraceOnThrow { 211 class ConcurrentModificationError extends Error {
235 /** The object that was modified in an incompatible way. */ 212 /** The object that was modified in an incompatible way. */
236 final Object modifiedObject; 213 final Object modifiedObject;
237 214
238 ConcurrentModificationError([this.modifiedObject]); 215 ConcurrentModificationError([this.modifiedObject]);
239 216
240 String toString() { 217 String toString() {
241 if (modifiedObject == null) { 218 if (modifiedObject == null) {
242 return "Concurrent modification during iteration."; 219 return "Concurrent modification during iteration.";
243 } 220 }
244 return "Concurrent modification during iteration: " 221 return "Concurrent modification during iteration: "
245 "${Error.safeToString(modifiedObject)}."; 222 "${Error.safeToString(modifiedObject)}.";
246 } 223 }
247 } 224 }
248 225
249 226
250 class OutOfMemoryError extends Error { 227 class OutOfMemoryError implements Error {
251 const OutOfMemoryError(); 228 const OutOfMemoryError();
252 String toString() => "Out of Memory"; 229 String toString() => "Out of Memory";
230
231 StackTrace get stackTrace => null;
253 } 232 }
254 233
255 234
256 class StackOverflowError extends Error { 235 class StackOverflowError implements Error {
257 const StackOverflowError(); 236 const StackOverflowError();
258 String toString() => "Stack Overflow"; 237 String toString() => "Stack Overflow";
238
239 StackTrace get stackTrace => null;
259 } 240 }
260 241
261 /** 242 /**
262 * Error thrown when a lazily initialized variable cannot be initialized. 243 * Error thrown when a lazily initialized variable cannot be initialized.
263 * 244 *
264 * A static/library variable with an initializer expression is initialized 245 * A static/library variable with an initializer expression is initialized
265 * the first time it is read. If evaluating the initializer expression causes 246 * the first time it is read. If evaluating the initializer expression causes
266 * another read of the variable, this error is thrown. 247 * another read of the variable, this error is thrown.
267 */ 248 */
268 class CyclicInitializationError extends Error with StackTraceOnThrow { 249 class CyclicInitializationError extends Error {
269 final String variableName; 250 final String variableName;
270 CyclicInitializationError([this.variableName]); 251 CyclicInitializationError([this.variableName]);
271 String toString() => variableName == null 252 String toString() => variableName == null
272 ? "Reading static variable during its initialization" 253 ? "Reading static variable during its initialization"
273 : "Reading static variable '$variableName' during its initialization"; 254 : "Reading static variable '$variableName' during its initialization";
274 } 255 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/lib/js_helper.dart ('k') | sdk/lib/json/json.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698