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

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

Issue 745573002: Create generic check methods for RangeError causing checks. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Typo and trailing whitespace. Created 6 years, 1 month 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 /** 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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 * description. 211 * description.
212 */ 212 */
213 RangeError.value(num value, [String name, String message]) 213 RangeError.value(num value, [String name, String message])
214 : start = null, end = null, 214 : start = null, end = null,
215 super.value(value, name, 215 super.value(value, name,
216 (message != null) ? message : "Value not in range"); 216 (message != null) ? message : "Value not in range");
217 217
218 /** 218 /**
219 * Create a new [RangeError] with for an invalid value being outside a range. 219 * Create a new [RangeError] with for an invalid value being outside a range.
220 * 220 *
221 * The allowed range is from [start] to [end], inclusive. 221 * The allowed range is from [minValue] to [maxValue], inclusive.
222 * If `start` or `end` are `null`, the range is infinite in that direction. 222 * If `minValue` or `maxValue` are `null`, the range is infinite in
223 * that direction.
223 * 224 *
224 * For a range from 0 to the length of something, end exclusive, use 225 * For a range from 0 to the length of something, end exclusive, use
225 * [RangeError.index]. 226 * [RangeError.index].
226 * 227 *
227 * An optional [name] can specify the argument name that has the 228 * An optional [name] can specify the argument name that has the
228 * invalid value, and the [message] can override the default error 229 * invalid value, and the [message] can override the default error
229 * description. 230 * description.
230 */ 231 */
231 RangeError.range(num invalidValue, this.start, this.end, 232 RangeError.range(num invalidValue, int minValue, int maxValue,
232 [String name, String message]) 233 [String name, String message])
233 : super.value(invalidValue, name, 234 : start = minValue,
235 end = maxValue,
236 super.value(invalidValue, name,
234 (message != null) ? message : "Invalid value"); 237 (message != null) ? message : "Invalid value");
235 238
236 /** 239 /**
237 * Creates a new [RangeError] stating that [index] is not a valid index 240 * Creates a new [RangeError] stating that [index] is not a valid index
238 * into [indexable]. 241 * into [indexable].
239 * 242 *
240 * An optional [name] can specify the argument name that has the 243 * An optional [name] can specify the argument name that has the
241 * invalid value, and the [message] can override the default error 244 * invalid value, and the [message] can override the default error
242 * description. 245 * description.
243 * 246 *
244 * The [length] is the length of [indexable] at the time of the error. 247 * The [length] is the length of [indexable] at the time of the error.
245 * If `length` is omitted, it defaults to `indexable.length`. 248 * If `length` is omitted, it defaults to `indexable.length`.
246 */ 249 */
247 factory RangeError.index(int index, indexable, 250 factory RangeError.index(int index, indexable,
248 [String name, 251 [String name,
249 String message, 252 String message,
250 int length]) = IndexError; 253 int length]) = IndexError;
251 254
255 /**
256 * Check that a [value] lies in a specific interval.
257 *
258 * Throws if [value] is not in the interval.
259 * The interval is from [minValue] to [maxValue], both inclusive.
260 */
261 static void checkValueInInterval(int value, int minValue, int maxValue,
262 [String name, String message]) {
263 if (value < minValue || value > maxValue) {
264 throw new RangeError.range(value, minValue, maxValue, name, message);
265 }
266 }
267
268 /**
269 * Check that a value is a valid index into an indexable object.
270 *
271 * Throws if [index] is not a valid index into [indexable].
272 *
273 * An indexable object is one that has a `length` and a and index-operator
274 * `[]` that accepts an index if `0 <= index < length`.
275 *
276 * If [length] is provided, it is used as the length of the indexable object,
277 * otherwise the length is found as `idexable.length`.
278 */
279 static void checkValidIndex(int index, var indexable,
280 [String name, int length, String message]) {
281 if (length == null) length = indexable.length;
282 if (index < 0 || index >= length) {
283 if (name == null) name = "index";
284 throw new RangeError.index(index, indexable, name, message, length);
285 }
286 }
287
288 /**
289 * Check that a range represents a slice of an indexable object.
290 *
291 * Throws if the range is not valid for an indexable object with
292 * the given [length].
293 * A range is valid for an indexable object with a given [length]
294 *
295 * if `0 <= [start] <= [end] <= [length]`.
296 * An `end` of `null` is considered equivalent to `length`.
297 *
298 * The [startName] and [endName] defaults to `"start"` and `"end"`,
299 * respectively.
300 */
301 static void checkValidRange(int start, int end, int length,
302 [String startName, String endName,
303 String message]) {
304 if (start < 0 || start > length) {
305 if (startName == null) startName = "start";
306 throw new RangeError.range(start, 0, length, startName, message);
307 }
308 if (end != null && (end < start || end > length)) {
309 if (endName == null) endName = "end";
310 throw new RangeError.range(end, start, length, endName, message);
311 }
312 }
313
314 /**
315 * Check that an integer value isn't negative.
316 *
317 * Throws if the value is negative.
318 */
319 static void checkNotNegative(int value, [String name, String message]) {
320 if (value < 0) throw new RangeError.range(value, 0, null, name, message);
321 }
322
252 String toString() { 323 String toString() {
253 if (!_hasValue) return "RangeError: $message"; 324 if (!_hasValue) return "RangeError: $message";
254 String value = Error.safeToString(invalidValue); 325 String value = Error.safeToString(invalidValue);
255 String explanation = ""; 326 String explanation = "";
256 if (start == null) { 327 if (start == null) {
257 if (end != null) { 328 if (end != null) {
258 explanation = ": Not less than or equal to $end"; 329 explanation = ": Not less than or equal to $end";
259 } 330 }
260 // If both are null, we don't add a description of the limits. 331 // If both are null, we don't add a description of the limits.
261 } else if (end == null) { 332 } else if (end == null) {
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 * the first time it is read. If evaluating the initializer expression causes 548 * the first time it is read. If evaluating the initializer expression causes
478 * another read of the variable, this error is thrown. 549 * another read of the variable, this error is thrown.
479 */ 550 */
480 class CyclicInitializationError extends Error { 551 class CyclicInitializationError extends Error {
481 final String variableName; 552 final String variableName;
482 CyclicInitializationError([this.variableName]); 553 CyclicInitializationError([this.variableName]);
483 String toString() => variableName == null 554 String toString() => variableName == null
484 ? "Reading static variable during its initialization" 555 ? "Reading static variable during its initialization"
485 : "Reading static variable '$variableName' during its initialization"; 556 : "Reading static variable '$variableName' during its initialization";
486 } 557 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698