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

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

Issue 18529003: Add stackTrace to Error object. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rebase 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
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);
33
34 external StackTrace get stackTrace;
35 } 35 }
36 36
37 /** 37 /**
38 * Error thrown by the runtime system when an assert statement fails. 38 * Error thrown by the runtime system when an assert statement fails.
39 */ 39 */
40 class AssertionError implements Error { 40 class AssertionError extends Error {
41 } 41 }
42 42
43 /** 43 /**
44 * Error thrown by the runtime system when a type assertion fails. 44 * Error thrown by the runtime system when a type assertion fails.
45 */ 45 */
46 class TypeError implements AssertionError { 46 class TypeError extends AssertionError {
47 } 47 }
48 48
49 /** 49 /**
50 * Error thrown by the runtime system when a cast operation fails. 50 * Error thrown by the runtime system when a cast operation fails.
51 */ 51 */
52 class CastError implements Error { 52 class CastError extends Error {
53 } 53 }
54 54
55 /** 55 /**
56 * Error thrown when attempting to throw [:null:]. 56 * Error thrown when attempting to throw [:null:].
57 */ 57 */
58 class NullThrownError implements Error { 58 class NullThrownError extends Error {
59 const NullThrownError(); 59 NullThrownError();
60 String toString() => "Throw of null."; 60 String toString() => "Throw of null.";
61 } 61 }
62 62
63 /** 63 /**
64 * Error thrown when a function is passed an unacceptable argument. 64 * Error thrown when a function is passed an unacceptable argument.
65 */ 65 */
66 class ArgumentError implements Error { 66 class ArgumentError extends Error {
67 final message; 67 final message;
68 68
69 /** The [message] describes the erroneous argument. */ 69 /** The [message] describes the erroneous argument. */
70 ArgumentError([this.message]); 70 ArgumentError([this.message]);
71 71
72 String toString() { 72 String toString() {
73 if (message != null) { 73 if (message != null) {
74 return "Illegal argument(s): $message"; 74 return "Illegal argument(s): $message";
75 } 75 }
76 return "Illegal argument(s)"; 76 return "Illegal argument(s)";
(...skipping 26 matching lines...) Expand all
103 103
104 104
105 /** 105 /**
106 * Error thrown when control reaches the end of a switch case. 106 * Error thrown when control reaches the end of a switch case.
107 * 107 *
108 * The Dart specification requires this error to be thrown when 108 * The Dart specification requires this error to be thrown when
109 * 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
110 * 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
111 * flow. 111 * flow.
112 */ 112 */
113 class FallThroughError implements Error { 113 class FallThroughError extends Error {
114 const FallThroughError(); 114 FallThroughError();
115 } 115 }
116 116
117 117
118 class AbstractClassInstantiationError implements Error { 118 class AbstractClassInstantiationError extends Error {
119 final String _className; 119 final String _className;
120 const AbstractClassInstantiationError(String this._className); 120 AbstractClassInstantiationError(String this._className);
121 String toString() => "Cannot instantiate abstract class: '$_className'"; 121 String toString() => "Cannot instantiate abstract class: '$_className'";
122 } 122 }
123 123
124 /** 124 /**
125 * Error thrown by the default implementation of [:noSuchMethod:] on [Object]. 125 * Error thrown by the default implementation of [:noSuchMethod:] on [Object].
126 */ 126 */
127 class NoSuchMethodError implements Error { 127 class NoSuchMethodError extends Error {
128 final Object _receiver; 128 final Object _receiver;
129 final String _memberName; 129 final String _memberName;
130 final List _arguments; 130 final List _arguments;
131 final Map<String,dynamic> _namedArguments; 131 final Map<String,dynamic> _namedArguments;
132 final List _existingArgumentNames; 132 final List _existingArgumentNames;
133 133
134 /** 134 /**
135 * Create a [NoSuchMethodError] corresponding to a failed method call. 135 * Create a [NoSuchMethodError] corresponding to a failed method call.
136 * 136 *
137 * 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.
138 * That is, the object on which the method was attempted called. 138 * That is, the object on which the method was attempted called.
139 * The second parameter is the name of the called method or accessor. 139 * The second parameter is the name of the called method or accessor.
140 * The third parameter is a list of the positional arguments that the method 140 * The third parameter is a list of the positional arguments that the method
141 * was called with. 141 * was called with.
142 * The fourth parameter is a map from [String] names to the values of named 142 * The fourth parameter is a map from [String] names to the values of named
143 * arguments that the method was called with. 143 * arguments that the method was called with.
144 * The optional [exisitingArgumentNames] is the expected parameters of a 144 * The optional [exisitingArgumentNames] is the expected parameters of a
145 * method with the same name on the receiver, if available. This is 145 * method with the same name on the receiver, if available. This is
146 * the method that would have been called if the parameters had matched. 146 * the method that would have been called if the parameters had matched.
147 */ 147 */
148 const NoSuchMethodError(Object this._receiver, 148 NoSuchMethodError(Object this._receiver,
149 String this._memberName, 149 String this._memberName,
150 List this._arguments, 150 List this._arguments,
151 Map<String,dynamic> this._namedArguments, 151 Map<String,dynamic> this._namedArguments,
152 [List existingArgumentNames = null]) 152 [List existingArgumentNames = null])
153 : this._existingArgumentNames = existingArgumentNames; 153 : this._existingArgumentNames = existingArgumentNames;
154 154
155 external String toString(); 155 external String toString();
156 } 156 }
157 157
158 158
159 /** 159 /**
160 * The operation was not allowed by the object. 160 * The operation was not allowed by the object.
161 * 161 *
162 * 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
163 * in its signature. 163 * in its signature.
164 */ 164 */
165 class UnsupportedError implements Error { 165 class UnsupportedError extends Error {
166 final String message; 166 final String message;
167 UnsupportedError(this.message); 167 UnsupportedError(this.message);
168 String toString() => "Unsupported operation: $message"; 168 String toString() => "Unsupported operation: $message";
169 } 169 }
170 170
171 171
172 /** 172 /**
173 * Thrown by operations that have not been implemented yet. 173 * Thrown by operations that have not been implemented yet.
174 * 174 *
175 * 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
176 * all the features it needs. 176 * all the features it needs.
177 * 177 *
178 * 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
179 * an [UnsupportedError] instead. This error is only intended for 179 * an [UnsupportedError] instead. This error is only intended for
180 * use during development. 180 * use during development.
181 */ 181 */
182 class UnimplementedError implements UnsupportedError { 182 class UnimplementedError extends Error implements UnsupportedError {
183 final String message; 183 final String message;
184 UnimplementedError([String this.message]); 184 UnimplementedError([String this.message]);
185 String toString() => (this.message != null 185 String toString() => (this.message != null
186 ? "UnimplementedError: $message" 186 ? "UnimplementedError: $message"
187 : "UnimplementedError"); 187 : "UnimplementedError");
188 } 188 }
189 189
190 190
191 /** 191 /**
192 * 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.
193 * 193 *
194 * 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
195 * actions. The message should be descriptive. 195 * actions. The message should be descriptive.
196 */ 196 */
197 class StateError implements Error { 197 class StateError extends Error {
198 final String message; 198 final String message;
199 StateError(this.message); 199 StateError(this.message);
200 String toString() => "Bad state: $message"; 200 String toString() => "Bad state: $message";
201 } 201 }
202 202
203 203
204 /** 204 /**
205 * Error occurring when a collection is modified during iteration. 205 * Error occurring when a collection is modified during iteration.
206 * 206 *
207 * Some modifications may be allowed for some collections, so each collection 207 * Some modifications may be allowed for some collections, so each collection
208 * ([Iterable] or similar collection of values) should declare which operations 208 * ([Iterable] or similar collection of values) should declare which operations
209 * are allowed during an iteration. 209 * are allowed during an iteration.
210 */ 210 */
211 class ConcurrentModificationError implements Error { 211 class ConcurrentModificationError extends Error {
212 /** The object that was modified in an incompatible way. */ 212 /** The object that was modified in an incompatible way. */
213 final Object modifiedObject; 213 final Object modifiedObject;
214 214
215 const ConcurrentModificationError([this.modifiedObject]); 215 ConcurrentModificationError([this.modifiedObject]);
216 216
217 String toString() { 217 String toString() {
218 if (modifiedObject == null) { 218 if (modifiedObject == null) {
219 return "Concurrent modification during iteration."; 219 return "Concurrent modification during iteration.";
220 } 220 }
221 return "Concurrent modification during iteration: " 221 return "Concurrent modification during iteration: "
222 "${Error.safeToString(modifiedObject)}."; 222 "${Error.safeToString(modifiedObject)}.";
223 } 223 }
224 } 224 }
225 225
226 226
227 class OutOfMemoryError implements Error { 227 class OutOfMemoryError implements Error {
228 const OutOfMemoryError(); 228 const OutOfMemoryError();
229 String toString() => "Out of Memory"; 229 String toString() => "Out of Memory";
230
231 StackTrace get stackTrace => null;
230 } 232 }
231 233
232 234
233 class StackOverflowError implements Error { 235 class StackOverflowError implements Error {
234 const StackOverflowError(); 236 const StackOverflowError();
235 String toString() => "Stack Overflow"; 237 String toString() => "Stack Overflow";
238
239 StackTrace get stackTrace => null;
236 } 240 }
237 241
238 /** 242 /**
239 * Error thrown when a lazily initialized variable cannot be initialized. 243 * Error thrown when a lazily initialized variable cannot be initialized.
240 * 244 *
241 * A static/library variable with an initializer expression is initialized 245 * A static/library variable with an initializer expression is initialized
242 * 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
243 * another read of the variable, this error is thrown. 247 * another read of the variable, this error is thrown.
244 */ 248 */
245 class CyclicInitializationError implements Error { 249 class CyclicInitializationError extends Error {
246 final String variableName; 250 final String variableName;
247 const CyclicInitializationError([this.variableName]); 251 CyclicInitializationError([this.variableName]);
248 String toString() => variableName == null 252 String toString() => variableName == null
249 ? "Reading static variable during its initialization" 253 ? "Reading static variable during its initialization"
250 : "Reading static variable '$variableName' during its initialization"; 254 : "Reading static variable '$variableName' during its initialization";
251 } 255 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698