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

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

Issue 2754013002: Format all dart: library files (Closed)
Patch Set: Created 3 years, 9 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
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 /** 7 /**
8 * Error objects thrown in the case of a program failure. 8 * Error objects thrown in the case of a program failure.
9 * 9 *
10 * An `Error` object represents a program failure that the programmer 10 * An `Error` object represents a program failure that the programmer
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 * 59 *
60 * Since errors are not created to be caught, 60 * Since errors are not created to be caught,
61 * there is no need for subclasses to distinguish the errors. 61 * there is no need for subclasses to distinguish the errors.
62 * Instead subclasses have been created in order to make groups 62 * Instead subclasses have been created in order to make groups
63 * of related errors easy to create with consistent error messages. 63 * of related errors easy to create with consistent error messages.
64 * For example, the [String.contains] method will use a [RangeError] 64 * For example, the [String.contains] method will use a [RangeError]
65 * if its `startIndex` isn't in the range `0..length`, 65 * if its `startIndex` isn't in the range `0..length`,
66 * which is easily created by `new RangeError.range(startIndex, 0, length)`. 66 * which is easily created by `new RangeError.range(startIndex, 0, length)`.
67 */ 67 */
68 class Error { 68 class Error {
69 Error(); // Prevent use as mixin. 69 Error(); // Prevent use as mixin.
70 70
71 /** 71 /**
72 * Safely convert a value to a [String] description. 72 * Safely convert a value to a [String] description.
73 * 73 *
74 * The conversion is guaranteed to not throw, so it won't use the object's 74 * The conversion is guaranteed to not throw, so it won't use the object's
75 * toString method. 75 * toString method.
76 */ 76 */
77 static String safeToString(Object object) { 77 static String safeToString(Object object) {
78 if (object is num || object is bool || null == object) { 78 if (object is num || object is bool || null == object) {
79 return object.toString(); 79 return object.toString();
(...skipping 18 matching lines...) Expand all
98 class AssertionError extends Error { 98 class AssertionError extends Error {
99 /** Message describing the assertion error. */ 99 /** Message describing the assertion error. */
100 final Object message; 100 final Object message;
101 AssertionError([this.message]); 101 AssertionError([this.message]);
102 String toString() => "Assertion failed"; 102 String toString() => "Assertion failed";
103 } 103 }
104 104
105 /** 105 /**
106 * Error thrown by the runtime system when a type assertion fails. 106 * Error thrown by the runtime system when a type assertion fails.
107 */ 107 */
108 class TypeError extends AssertionError { 108 class TypeError extends AssertionError {}
109 }
110 109
111 /** 110 /**
112 * Error thrown by the runtime system when a cast operation fails. 111 * Error thrown by the runtime system when a cast operation fails.
113 */ 112 */
114 class CastError extends Error { 113 class CastError extends Error {}
115 }
116 114
117 /** 115 /**
118 * Error thrown when attempting to throw [:null:]. 116 * Error thrown when attempting to throw [:null:].
119 */ 117 */
120 class NullThrownError extends Error { 118 class NullThrownError extends Error {
121 String toString() => "Throw of null."; 119 String toString() => "Throw of null.";
122 } 120 }
123 121
124 /** 122 /**
125 * Error thrown when a function is passed an unacceptable argument. 123 * Error thrown when a function is passed an unacceptable argument.
126 */ 124 */
127 class ArgumentError extends Error { 125 class ArgumentError extends Error {
128 /** Whether value was provided. */ 126 /** Whether value was provided. */
129 final bool _hasValue; 127 final bool _hasValue;
130 /** The invalid value. */ 128 /** The invalid value. */
131 final invalidValue; 129 final invalidValue;
132 /** Name of the invalid argument, if available. */ 130 /** Name of the invalid argument, if available. */
133 final String name; 131 final String name;
134 /** Message describing the problem. */ 132 /** Message describing the problem. */
135 final message; 133 final message;
136 134
137 /** 135 /**
138 * The [message] describes the erroneous argument. 136 * The [message] describes the erroneous argument.
139 * 137 *
140 * Existing code may be using `message` to hold the invalid value. 138 * Existing code may be using `message` to hold the invalid value.
141 * If the `message` is not a [String], it is assumed to be a value instead 139 * If the `message` is not a [String], it is assumed to be a value instead
142 * of a message. 140 * of a message.
143 */ 141 */
144 ArgumentError([this.message]) 142 ArgumentError([this.message])
145 : invalidValue = null, 143 : invalidValue = null,
146 _hasValue = false, 144 _hasValue = false,
147 name = null; 145 name = null;
148 146
149 /** 147 /**
150 * Creates error containing the invalid [value]. 148 * Creates error containing the invalid [value].
151 * 149 *
152 * A message is built by suffixing the [message] argument with 150 * A message is built by suffixing the [message] argument with
153 * the [name] argument (if provided) and the value. Example 151 * the [name] argument (if provided) and the value. Example
154 * 152 *
155 * "Invalid argument (foo): null" 153 * "Invalid argument (foo): null"
156 * 154 *
157 * The `name` should match the argument name of the function, but if 155 * The `name` should match the argument name of the function, but if
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 final num start; 196 final num start;
199 /** The maximum value that [value] is allowed to assume. */ 197 /** The maximum value that [value] is allowed to assume. */
200 final num end; 198 final num end;
201 199
202 // TODO(lrn): This constructor should be called only with string values. 200 // TODO(lrn): This constructor should be called only with string values.
203 // It currently isn't in all cases. 201 // It currently isn't in all cases.
204 /** 202 /**
205 * Create a new [RangeError] with the given [message]. 203 * Create a new [RangeError] with the given [message].
206 */ 204 */
207 RangeError(var message) 205 RangeError(var message)
208 : start = null, end = null, super(message); 206 : start = null,
207 end = null,
208 super(message);
209 209
210 /** 210 /**
211 * Create a new [RangeError] with a message for the given [value]. 211 * Create a new [RangeError] with a message for the given [value].
212 * 212 *
213 * An optional [name] can specify the argument name that has the 213 * An optional [name] can specify the argument name that has the
214 * invalid value, and the [message] can override the default error 214 * invalid value, and the [message] can override the default error
215 * description. 215 * description.
216 */ 216 */
217 RangeError.value(num value, [String name, String message]) 217 RangeError.value(num value, [String name, String message])
218 : start = null, end = null, 218 : start = null,
219 super.value(value, name, 219 end = null,
220 (message != null) ? message : "Value not in range"); 220 super.value(
221 value, name, (message != null) ? message : "Value not in range");
221 222
222 /** 223 /**
223 * Create a new [RangeError] for a value being outside the valid range. 224 * Create a new [RangeError] for a value being outside the valid range.
224 * 225 *
225 * The allowed range is from [minValue] to [maxValue], inclusive. 226 * The allowed range is from [minValue] to [maxValue], inclusive.
226 * If `minValue` or `maxValue` are `null`, the range is infinite in 227 * If `minValue` or `maxValue` are `null`, the range is infinite in
227 * that direction. 228 * that direction.
228 * 229 *
229 * For a range from 0 to the length of something, end exclusive, use 230 * For a range from 0 to the length of something, end exclusive, use
230 * [RangeError.index]. 231 * [RangeError.index].
231 * 232 *
232 * An optional [name] can specify the argument name that has the 233 * An optional [name] can specify the argument name that has the
233 * invalid value, and the [message] can override the default error 234 * invalid value, and the [message] can override the default error
234 * description. 235 * description.
235 */ 236 */
236 RangeError.range(num invalidValue, int minValue, int maxValue, 237 RangeError.range(num invalidValue, int minValue, int maxValue,
237 [String name, String message]) 238 [String name, String message])
238 : start = minValue, 239 : start = minValue,
239 end = maxValue, 240 end = maxValue,
240 super.value(invalidValue, name, 241 super.value(
241 (message != null) ? message : "Invalid value"); 242 invalidValue, name, (message != null) ? message : "Invalid value");
242 243
243 /** 244 /**
244 * Creates a new [RangeError] stating that [index] is not a valid index 245 * Creates a new [RangeError] stating that [index] is not a valid index
245 * into [indexable]. 246 * into [indexable].
246 * 247 *
247 * An optional [name] can specify the argument name that has the 248 * An optional [name] can specify the argument name that has the
248 * invalid value, and the [message] can override the default error 249 * invalid value, and the [message] can override the default error
249 * description. 250 * description.
250 * 251 *
251 * The [length] is the length of [indexable] at the time of the error. 252 * The [length] is the length of [indexable] at the time of the error.
252 * If `length` is omitted, it defaults to `indexable.length`. 253 * If `length` is omitted, it defaults to `indexable.length`.
253 */ 254 */
254 factory RangeError.index(int index, indexable, 255 factory RangeError.index(int index, indexable,
255 [String name, 256 [String name, String message, int length]) = IndexError;
256 String message,
257 int length]) = IndexError;
258 257
259 /** 258 /**
260 * Check that a [value] lies in a specific interval. 259 * Check that a [value] lies in a specific interval.
261 * 260 *
262 * Throws if [value] is not in the interval. 261 * Throws if [value] is not in the interval.
263 * The interval is from [minValue] to [maxValue], both inclusive. 262 * The interval is from [minValue] to [maxValue], both inclusive.
264 */ 263 */
265 static void checkValueInInterval(int value, int minValue, int maxValue, 264 static void checkValueInInterval(int value, int minValue, int maxValue,
266 [String name, String message]) { 265 [String name, String message]) {
267 if (value < minValue || value > maxValue) { 266 if (value < minValue || value > maxValue) {
268 throw new RangeError.range(value, minValue, maxValue, name, message); 267 throw new RangeError.range(value, minValue, maxValue, name, message);
269 } 268 }
270 } 269 }
271 270
272 /** 271 /**
273 * Check that a value is a valid index into an indexable object. 272 * Check that a value is a valid index into an indexable object.
274 * 273 *
275 * Throws if [index] is not a valid index into [indexable]. 274 * Throws if [index] is not a valid index into [indexable].
276 * 275 *
277 * An indexable object is one that has a `length` and a and index-operator 276 * An indexable object is one that has a `length` and a and index-operator
278 * `[]` that accepts an index if `0 <= index < length`. 277 * `[]` that accepts an index if `0 <= index < length`.
279 * 278 *
280 * If [length] is provided, it is used as the length of the indexable object, 279 * If [length] is provided, it is used as the length of the indexable object,
281 * otherwise the length is found as `indexable.length`. 280 * otherwise the length is found as `indexable.length`.
282 */ 281 */
283 static void checkValidIndex(int index, var indexable, 282 static void checkValidIndex(int index, var indexable,
284 [String name, int length, String message]) { 283 [String name, int length, String message]) {
285 if (length == null) length = indexable.length; 284 if (length == null) length = indexable.length;
286 // Comparing with `0` as receiver produces better dart2js type inference. 285 // Comparing with `0` as receiver produces better dart2js type inference.
287 if (0 > index || index >= length) { 286 if (0 > index || index >= length) {
288 if (name == null) name = "index"; 287 if (name == null) name = "index";
289 throw new RangeError.index(index, indexable, name, message, length); 288 throw new RangeError.index(index, indexable, name, message, length);
290 } 289 }
291 } 290 }
292 291
293 /** 292 /**
294 * Check that a range represents a slice of an indexable object. 293 * Check that a range represents a slice of an indexable object.
295 * 294 *
296 * Throws if the range is not valid for an indexable object with 295 * Throws if the range is not valid for an indexable object with
297 * the given [length]. 296 * the given [length].
298 * A range is valid for an indexable object with a given [length] 297 * A range is valid for an indexable object with a given [length]
299 * 298 *
300 * if `0 <= [start] <= [end] <= [length]`. 299 * if `0 <= [start] <= [end] <= [length]`.
301 * An `end` of `null` is considered equivalent to `length`. 300 * An `end` of `null` is considered equivalent to `length`.
302 * 301 *
303 * The [startName] and [endName] defaults to `"start"` and `"end"`, 302 * The [startName] and [endName] defaults to `"start"` and `"end"`,
304 * respectively. 303 * respectively.
305 * 304 *
306 * Returns the actual `end` value, which is `length` if `end` is `null`, 305 * Returns the actual `end` value, which is `length` if `end` is `null`,
307 * and `end` otherwise. 306 * and `end` otherwise.
308 */ 307 */
309 static int checkValidRange(int start, int end, int length, 308 static int checkValidRange(int start, int end, int length,
310 [String startName, String endName, 309 [String startName, String endName, String message]) {
311 String message]) {
312 // Comparing with `0` as receiver produces better dart2js type inference. 310 // Comparing with `0` as receiver produces better dart2js type inference.
313 // Ditto `start > end` below. 311 // Ditto `start > end` below.
314 if (0 > start || start > length) { 312 if (0 > start || start > length) {
315 if (startName == null) startName = "start"; 313 if (startName == null) startName = "start";
316 throw new RangeError.range(start, 0, length, startName, message); 314 throw new RangeError.range(start, 0, length, startName, message);
317 } 315 }
318 if (end != null) { 316 if (end != null) {
319 if (start > end || end > length) { 317 if (start > end || end > length) {
320 if (endName == null) endName = "end"; 318 if (endName == null) endName = "end";
321 throw new RangeError.range(end, start, length, endName, message); 319 throw new RangeError.range(end, start, length, endName, message);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 /** 371 /**
374 * Creates a new [IndexError] stating that [invalidValue] is not a valid index 372 * Creates a new [IndexError] stating that [invalidValue] is not a valid index
375 * into [indexable]. 373 * into [indexable].
376 * 374 *
377 * The [length] is the length of [indexable] at the time of the error. 375 * The [length] is the length of [indexable] at the time of the error.
378 * If `length` is omitted, it defaults to `indexable.length`. 376 * If `length` is omitted, it defaults to `indexable.length`.
379 * 377 *
380 * The message is used as part of the string representation of the error. 378 * The message is used as part of the string representation of the error.
381 */ 379 */
382 IndexError(int invalidValue, indexable, 380 IndexError(int invalidValue, indexable,
383 [String name, String message, int length]) 381 [String name, String message, int length])
384 : this.indexable = indexable, 382 : this.indexable = indexable,
385 this.length = (length != null) ? length : indexable.length, 383 this.length = (length != null) ? length : indexable.length,
386 super.value(invalidValue, name, 384 super.value(invalidValue, name,
387 (message != null) ? message : "Index out of range"); 385 (message != null) ? message : "Index out of range");
388 386
389 // Getters inherited from RangeError. 387 // Getters inherited from RangeError.
390 int get start => 0; 388 int get start => 0;
391 int get end => length - 1; 389 int get end => length - 1;
392 390
393 String get _errorName => "RangeError"; 391 String get _errorName => "RangeError";
394 String get _errorExplanation { 392 String get _errorExplanation {
395 assert(_hasValue); 393 assert(_hasValue);
396 if (invalidValue < 0) { 394 if (invalidValue < 0) {
397 return ": index must not be negative"; 395 return ": index must not be negative";
398 } 396 }
399 if (length == 0) { 397 if (length == 0) {
400 return ": no indices are valid"; 398 return ": no indices are valid";
401 } 399 }
402 return ": index should be less than $length"; 400 return ": index should be less than $length";
403 } 401 }
404 } 402 }
405 403
406
407 /** 404 /**
408 * Error thrown when control reaches the end of a switch case. 405 * Error thrown when control reaches the end of a switch case.
409 * 406 *
410 * The Dart specification requires this error to be thrown when 407 * The Dart specification requires this error to be thrown when
411 * control reaches the end of a switch case (except the last case 408 * control reaches the end of a switch case (except the last case
412 * of a switch) without meeting a break or similar end of the control 409 * of a switch) without meeting a break or similar end of the control
413 * flow. 410 * flow.
414 */ 411 */
415 class FallThroughError extends Error { 412 class FallThroughError extends Error {
416 FallThroughError(); 413 FallThroughError();
417 414
418 external String toString(); 415 external String toString();
419 } 416 }
420 417
421 /** 418 /**
422 * Error thrown when trying to instantiate an abstract class. 419 * Error thrown when trying to instantiate an abstract class.
423 */ 420 */
424 class AbstractClassInstantiationError extends Error { 421 class AbstractClassInstantiationError extends Error {
425 final String _className; 422 final String _className;
426 AbstractClassInstantiationError(String className) : _className = className; 423 AbstractClassInstantiationError(String className) : _className = className;
427 424
428 external String toString(); 425 external String toString();
429 } 426 }
430 427
431
432 /** 428 /**
433 * Error thrown by the default implementation of [:noSuchMethod:] on [Object]. 429 * Error thrown by the default implementation of [:noSuchMethod:] on [Object].
434 */ 430 */
435 class NoSuchMethodError extends Error { 431 class NoSuchMethodError extends Error {
436 final Object _receiver; 432 final Object _receiver;
437 final Symbol _memberName; 433 final Symbol _memberName;
438 final List _arguments; 434 final List _arguments;
439 final Map<Symbol, dynamic> _namedArguments; 435 final Map<Symbol, dynamic> _namedArguments;
440 final List _existingArgumentNames; 436 final List _existingArgumentNames;
441 437
(...skipping 13 matching lines...) Expand all
455 * empty list. 451 * empty list.
456 * 452 *
457 * The [namedArguments] is a map from [Symbol]s to the values of named 453 * The [namedArguments] is a map from [Symbol]s to the values of named
458 * arguments that the method was called with. 454 * arguments that the method was called with.
459 * 455 *
460 * The optional [existingArgumentNames] is the expected parameters of a 456 * The optional [existingArgumentNames] is the expected parameters of a
461 * method with the same name on the receiver, if available. This is 457 * method with the same name on the receiver, if available. This is
462 * the signature of the method that would have been called if the parameters 458 * the signature of the method that would have been called if the parameters
463 * had matched. 459 * had matched.
464 */ 460 */
465 external NoSuchMethodError(Object receiver, 461 external NoSuchMethodError(Object receiver, Symbol memberName,
466 Symbol memberName, 462 List positionalArguments, Map<Symbol, dynamic> namedArguments,
467 List positionalArguments, 463 [List existingArgumentNames = null]);
468 Map<Symbol, dynamic> namedArguments,
469 [List existingArgumentNames = null]);
470 464
471 external String toString(); 465 external String toString();
472 } 466 }
473 467
474
475 /** 468 /**
476 * The operation was not allowed by the object. 469 * The operation was not allowed by the object.
477 * 470 *
478 * This [Error] is thrown when an instance cannot implement one of the methods 471 * This [Error] is thrown when an instance cannot implement one of the methods
479 * in its signature. 472 * in its signature.
480 */ 473 */
481 class UnsupportedError extends Error { 474 class UnsupportedError extends Error {
482 final String message; 475 final String message;
483 UnsupportedError(this.message); 476 UnsupportedError(this.message);
484 String toString() => "Unsupported operation: $message"; 477 String toString() => "Unsupported operation: $message";
485 } 478 }
486 479
487
488 /** 480 /**
489 * Thrown by operations that have not been implemented yet. 481 * Thrown by operations that have not been implemented yet.
490 * 482 *
491 * This [Error] is thrown by unfinished code that hasn't yet implemented 483 * This [Error] is thrown by unfinished code that hasn't yet implemented
492 * all the features it needs. 484 * all the features it needs.
493 * 485 *
494 * If a class is not intending to implement the feature, it should throw 486 * If a class is not intending to implement the feature, it should throw
495 * an [UnsupportedError] instead. This error is only intended for 487 * an [UnsupportedError] instead. This error is only intended for
496 * use during development. 488 * use during development.
497 */ 489 */
498 class UnimplementedError extends Error implements UnsupportedError { 490 class UnimplementedError extends Error implements UnsupportedError {
499 final String message; 491 final String message;
500 UnimplementedError([this.message]); 492 UnimplementedError([this.message]);
501 String toString() => (this.message != null 493 String toString() => (this.message != null
502 ? "UnimplementedError: $message" 494 ? "UnimplementedError: $message"
503 : "UnimplementedError"); 495 : "UnimplementedError");
504 } 496 }
505 497
506
507 /** 498 /**
508 * The operation was not allowed by the current state of the object. 499 * The operation was not allowed by the current state of the object.
509 * 500 *
510 * This is a generic error used for a variety of different erroneous 501 * This is a generic error used for a variety of different erroneous
511 * actions. The message should be descriptive. 502 * actions. The message should be descriptive.
512 */ 503 */
513 class StateError extends Error { 504 class StateError extends Error {
514 final String message; 505 final String message;
515 StateError(this.message); 506 StateError(this.message);
516 String toString() => "Bad state: $message"; 507 String toString() => "Bad state: $message";
517 } 508 }
518 509
519
520 /** 510 /**
521 * Error occurring when a collection is modified during iteration. 511 * Error occurring when a collection is modified during iteration.
522 * 512 *
523 * Some modifications may be allowed for some collections, so each collection 513 * Some modifications may be allowed for some collections, so each collection
524 * ([Iterable] or similar collection of values) should declare which operations 514 * ([Iterable] or similar collection of values) should declare which operations
525 * are allowed during an iteration. 515 * are allowed during an iteration.
526 */ 516 */
527 class ConcurrentModificationError extends Error { 517 class ConcurrentModificationError extends Error {
528 /** The object that was modified in an incompatible way. */ 518 /** The object that was modified in an incompatible way. */
529 final Object modifiedObject; 519 final Object modifiedObject;
530 520
531 ConcurrentModificationError([this.modifiedObject]); 521 ConcurrentModificationError([this.modifiedObject]);
532 522
533 String toString() { 523 String toString() {
534 if (modifiedObject == null) { 524 if (modifiedObject == null) {
535 return "Concurrent modification during iteration."; 525 return "Concurrent modification during iteration.";
536 } 526 }
537 return "Concurrent modification during iteration: " 527 return "Concurrent modification during iteration: "
538 "${Error.safeToString(modifiedObject)}."; 528 "${Error.safeToString(modifiedObject)}.";
539 } 529 }
540 } 530 }
541 531
542
543 class OutOfMemoryError implements Error { 532 class OutOfMemoryError implements Error {
544 const OutOfMemoryError(); 533 const OutOfMemoryError();
545 String toString() => "Out of Memory"; 534 String toString() => "Out of Memory";
546 535
547 StackTrace get stackTrace => null; 536 StackTrace get stackTrace => null;
548 } 537 }
549 538
550
551 class StackOverflowError implements Error { 539 class StackOverflowError implements Error {
552 const StackOverflowError(); 540 const StackOverflowError();
553 String toString() => "Stack Overflow"; 541 String toString() => "Stack Overflow";
554 542
555 StackTrace get stackTrace => null; 543 StackTrace get stackTrace => null;
556 } 544 }
557 545
558 /** 546 /**
559 * Error thrown when a lazily initialized variable cannot be initialized. 547 * Error thrown when a lazily initialized variable cannot be initialized.
560 * 548 *
561 * A static/library variable with an initializer expression is initialized 549 * A static/library variable with an initializer expression is initialized
562 * the first time it is read. If evaluating the initializer expression causes 550 * the first time it is read. If evaluating the initializer expression causes
563 * another read of the variable, this error is thrown. 551 * another read of the variable, this error is thrown.
564 */ 552 */
565 class CyclicInitializationError extends Error { 553 class CyclicInitializationError extends Error {
566 final String variableName; 554 final String variableName;
567 CyclicInitializationError([this.variableName]); 555 CyclicInitializationError([this.variableName]);
568 String toString() => variableName == null 556 String toString() => variableName == null
569 ? "Reading static variable during its initialization" 557 ? "Reading static variable during its initialization"
570 : "Reading static variable '$variableName' during its initialization"; 558 : "Reading static variable '$variableName' during its initialization";
571 } 559 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698