| OLD | NEW |
| 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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 * | 150 * |
| 151 * "Invalid argument (foo): null" | 151 * "Invalid argument (foo): null" |
| 152 * | 152 * |
| 153 * The `name` should match the argument name of the function, but if | 153 * The `name` should match the argument name of the function, but if |
| 154 * the function is a method implementing an interface, and its argument | 154 * the function is a method implementing an interface, and its argument |
| 155 * names differ from the interface, it might be more useful to use the | 155 * names differ from the interface, it might be more useful to use the |
| 156 * interface method's argument name (or just rename arguments to match). | 156 * interface method's argument name (or just rename arguments to match). |
| 157 */ | 157 */ |
| 158 ArgumentError.value(value, | 158 ArgumentError.value(value, |
| 159 [String this.name, | 159 [String this.name, |
| 160 String this.message = "Invalid argument"]) | 160 String this.message]) |
| 161 : invalidValue = value, | 161 : invalidValue = value, |
| 162 _hasValue = true; | 162 _hasValue = true; |
| 163 | 163 |
| 164 /** | 164 /** |
| 165 * Create an argument error for a `null` argument that must not be `null`. | 165 * Create an argument error for a `null` argument that must not be `null`. |
| 166 * | 166 * |
| 167 * Shorthand for calling [ArgumentError.value] with a `null` value and a | 167 * Shorthand for calling [ArgumentError.value] with a `null` value and a |
| 168 * message of `"Must not be null"`. | 168 * message of `"Must not be null"`. |
| 169 */ | 169 */ |
| 170 ArgumentError.notNull([String name]) | 170 ArgumentError.notNull([String name]) |
| 171 : this.value(null, name, "Must not be null"); | 171 : this.value(null, name, "Must not be null"); |
| 172 | 172 |
| 173 // Helper functions for toString overridden in subclasses. |
| 174 String get _errorName => "Invalid argument${name == null ? "(s)" : ""}"; |
| 175 String get _errorExplanation => ""; |
| 176 |
| 173 String toString() { | 177 String toString() { |
| 174 if (!_hasValue) { | |
| 175 var result = "Invalid arguments(s)"; | |
| 176 if (message != null) { | |
| 177 result = "$result: $message"; | |
| 178 } | |
| 179 return result; | |
| 180 } | |
| 181 String nameString = ""; | 178 String nameString = ""; |
| 182 if (name != null) { | 179 if (name != null) { |
| 183 nameString = " ($name)"; | 180 nameString = " ($name)"; |
| 184 } | 181 } |
| 185 return "$message$nameString: ${Error.safeToString(invalidValue)}"; | 182 var message = this.message == null ? "" : ": ${this.message}"; |
| 183 String prefix = "$_errorName$nameString$message"; |
| 184 if (invalidValue == null) return prefix; |
| 185 // If we know the invalid value, we can try to describe the problem. |
| 186 String explanation = _errorExplanation; |
| 187 String errorValue = Error.safeToString(invalidValue); |
| 188 return "$prefix$explanation: $errorValue"; |
| 186 } | 189 } |
| 187 } | 190 } |
| 188 | 191 |
| 189 /** | 192 /** |
| 190 * Error thrown due to an index being outside a valid range. | 193 * Error thrown due to an index being outside a valid range. |
| 191 */ | 194 */ |
| 192 class RangeError extends ArgumentError { | 195 class RangeError extends ArgumentError { |
| 193 /** The minimum value that [value] is allowed to assume. */ | 196 /** The minimum value that [value] is allowed to assume. */ |
| 194 final num start; | 197 final num start; |
| 195 /** The maximum value that [value] is allowed to assume. */ | 198 /** The maximum value that [value] is allowed to assume. */ |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 270 |
| 268 /** | 271 /** |
| 269 * 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. |
| 270 * | 273 * |
| 271 * Throws if [index] is not a valid index into [indexable]. | 274 * Throws if [index] is not a valid index into [indexable]. |
| 272 * | 275 * |
| 273 * 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 |
| 274 * `[]` that accepts an index if `0 <= index < length`. | 277 * `[]` that accepts an index if `0 <= index < length`. |
| 275 * | 278 * |
| 276 * 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, |
| 277 * otherwise the length is found as `idexable.length`. | 280 * otherwise the length is found as `indexable.length`. |
| 278 */ | 281 */ |
| 279 static void checkValidIndex(int index, var indexable, | 282 static void checkValidIndex(int index, var indexable, |
| 280 [String name, int length, String message]) { | 283 [String name, int length, String message]) { |
| 281 if (length == null) length = indexable.length; | 284 if (length == null) length = indexable.length; |
| 282 if (index < 0 || index >= length) { | 285 if (index < 0 || index >= length) { |
| 283 if (name == null) name = "index"; | 286 if (name == null) name = "index"; |
| 284 throw new RangeError.index(index, indexable, name, message, length); | 287 throw new RangeError.index(index, indexable, name, message, length); |
| 285 } | 288 } |
| 286 } | 289 } |
| 287 | 290 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 313 | 316 |
| 314 /** | 317 /** |
| 315 * Check that an integer value isn't negative. | 318 * Check that an integer value isn't negative. |
| 316 * | 319 * |
| 317 * Throws if the value is negative. | 320 * Throws if the value is negative. |
| 318 */ | 321 */ |
| 319 static void checkNotNegative(int value, [String name, String message]) { | 322 static void checkNotNegative(int value, [String name, String message]) { |
| 320 if (value < 0) throw new RangeError.range(value, 0, null, name, message); | 323 if (value < 0) throw new RangeError.range(value, 0, null, name, message); |
| 321 } | 324 } |
| 322 | 325 |
| 323 String toString() { | 326 String get _errorName => "RangeError"; |
| 324 if (!_hasValue) return "RangeError: $message"; | 327 String get _errorExplanation { |
| 325 String value = Error.safeToString(invalidValue); | 328 assert(_hasValue); |
| 326 String explanation = ""; | 329 String explanation = ""; |
| 327 if (start == null) { | 330 if (start == null) { |
| 328 if (end != null) { | 331 if (end != null) { |
| 329 explanation = ": Not less than or equal to $end"; | 332 explanation = ": Not less than or equal to $end"; |
| 330 } | 333 } |
| 331 // If both are null, we don't add a description of the limits. | 334 // If both are null, we don't add a description of the limits. |
| 332 } else if (end == null) { | 335 } else if (end == null) { |
| 333 explanation = ": Not greater than or equal to $start"; | 336 explanation = ": Not greater than or equal to $start"; |
| 334 } else if (end > start) { | 337 } else if (end > start) { |
| 335 explanation = ": Not in range $start..$end, inclusive."; | 338 explanation = ": Not in range $start..$end, inclusive"; |
| 336 } else if (end < start) { | 339 } else if (end < start) { |
| 337 explanation = ": Valid value range is empty"; | 340 explanation = ": Valid value range is empty"; |
| 338 } else { | 341 } else { |
| 339 // end == start. | 342 // end == start. |
| 340 explanation = ": Only valid value is $start"; | 343 explanation = ": Only valid value is $start"; |
| 341 } | 344 } |
| 342 return "RangeError: $message ($value)$explanation"; | 345 return explanation; |
| 343 } | 346 } |
| 344 } | 347 } |
| 345 | 348 |
| 346 /** | 349 /** |
| 347 * A specialized [RangeError] used when an index is not in the range | 350 * A specialized [RangeError] used when an index is not in the range |
| 348 * `0..indexable.length-1`. | 351 * `0..indexable.length-1`. |
| 349 * | 352 * |
| 350 * Also contains the indexable object, its length at the time of the error, | 353 * Also contains the indexable object, its length at the time of the error, |
| 351 * and the invalid index itself. | 354 * and the invalid index itself. |
| 352 */ | 355 */ |
| (...skipping 16 matching lines...) Expand all Loading... |
| 369 [String name, String message, int length]) | 372 [String name, String message, int length]) |
| 370 : this.indexable = indexable, | 373 : this.indexable = indexable, |
| 371 this.length = (length != null) ? length : indexable.length, | 374 this.length = (length != null) ? length : indexable.length, |
| 372 super.value(invalidValue, name, | 375 super.value(invalidValue, name, |
| 373 (message != null) ? message : "Index out of range"); | 376 (message != null) ? message : "Index out of range"); |
| 374 | 377 |
| 375 // Getters inherited from RangeError. | 378 // Getters inherited from RangeError. |
| 376 int get start => 0; | 379 int get start => 0; |
| 377 int get end => length - 1; | 380 int get end => length - 1; |
| 378 | 381 |
| 379 String toString() { | 382 String get _errorName => "RangeError"; |
| 383 String get _errorExplanation { |
| 380 assert(_hasValue); | 384 assert(_hasValue); |
| 381 String target = Error.safeToString(indexable); | 385 String target = Error.safeToString(indexable); |
| 382 var explanation = "index should be less than $length"; | 386 var explanation = ": index should be less than $length"; |
| 383 if (invalidValue < 0) { | 387 if (invalidValue < 0) { |
| 384 explanation = "index must not be negative"; | 388 explanation = ": index must not be negative"; |
| 385 } | 389 } |
| 386 return "RangeError: $message ($target[$invalidValue]): $explanation"; | 390 return explanation; |
| 387 } | 391 } |
| 388 } | 392 } |
| 389 | 393 |
| 390 | 394 |
| 391 /** | 395 /** |
| 392 * Error thrown when control reaches the end of a switch case. | 396 * Error thrown when control reaches the end of a switch case. |
| 393 * | 397 * |
| 394 * The Dart specification requires this error to be thrown when | 398 * The Dart specification requires this error to be thrown when |
| 395 * control reaches the end of a switch case (except the last case | 399 * control reaches the end of a switch case (except the last case |
| 396 * of a switch) without meeting a break or similar end of the control | 400 * of a switch) without meeting a break or similar end of the control |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 * the first time it is read. If evaluating the initializer expression causes | 552 * the first time it is read. If evaluating the initializer expression causes |
| 549 * another read of the variable, this error is thrown. | 553 * another read of the variable, this error is thrown. |
| 550 */ | 554 */ |
| 551 class CyclicInitializationError extends Error { | 555 class CyclicInitializationError extends Error { |
| 552 final String variableName; | 556 final String variableName; |
| 553 CyclicInitializationError([this.variableName]); | 557 CyclicInitializationError([this.variableName]); |
| 554 String toString() => variableName == null | 558 String toString() => variableName == null |
| 555 ? "Reading static variable during its initialization" | 559 ? "Reading static variable during its initialization" |
| 556 : "Reading static variable '$variableName' during its initialization"; | 560 : "Reading static variable '$variableName' during its initialization"; |
| 557 } | 561 } |
| OLD | NEW |