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

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

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

Powered by Google App Engine
This is Rietveld 408576698