Chromium Code Reviews| 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 * Error thrown when attempting to throw [:null:]. | 152 * Error thrown when attempting to throw [:null:]. |
| 153 */ | 153 */ |
| 154 class NullThrownError extends Error { | 154 class NullThrownError extends Error { |
| 155 String toString() => "Throw of null."; | 155 String toString() => "Throw of null."; |
| 156 } | 156 } |
| 157 | 157 |
| 158 /** | 158 /** |
| 159 * Error thrown when a function is passed an unacceptable argument. | 159 * Error thrown when a function is passed an unacceptable argument. |
| 160 */ | 160 */ |
| 161 class ArgumentError extends Error { | 161 class ArgumentError extends Error { |
| 162 /** Whether value was provided. */ | |
| 163 final bool _hasValue; | |
| 164 /** The invalid value. */ | |
| 165 final invalidValue; | |
| 166 /** Name of the invalid argument, if available. */ | |
| 167 final String name; | |
| 168 /** Message describing the problem. */ | |
| 162 final message; | 169 final message; |
| 163 | 170 |
| 164 /** The [message] describes the erroneous argument. */ | 171 /** |
| 165 ArgumentError([this.message]); | 172 * The [message] describes the erroneous argument. |
| 173 * | |
| 174 * Existing code may be using `message` to hold the invalid value. | |
| 175 * If the `message` is not a [String], it is assumed to be a value instead | |
| 176 * of a message. | |
| 177 */ | |
| 178 ArgumentError([this.message]) | |
| 179 : invalidValue = null, | |
| 180 _hasValue = false, | |
| 181 name = null; | |
| 182 | |
| 183 /** | |
| 184 * Creates error containing the invalid [value]. | |
| 185 * | |
| 186 * A message is built by suffixing the [message] argument with | |
| 187 * the [name] argument (if provided) and the value. Example | |
| 188 * | |
| 189 * "Invalid argument (foo): null" | |
| 190 * | |
| 191 * The `name` should match the argument name of the function, but if | |
| 192 * the function is a method implementing an interface, and its argument | |
| 193 * names differ from the interface, it might be more useful to use the | |
| 194 * interface method's argument name (or just rename arguments to match). | |
| 195 */ | |
| 196 ArgumentError.value(value, | |
| 197 [String this.name, | |
| 198 String this.message = "Invalid argument"]) | |
| 199 : invalidValue = value, | |
| 200 _hasValue = true; | |
| 201 | |
| 202 /** | |
| 203 * Create an argument error for a `null` argument that must not be `null`. | |
| 204 * | |
| 205 * Shorthand for calling [ArgumentError.value] with a `null` value and a | |
| 206 * message of `"Must not be null"`. | |
| 207 */ | |
| 208 ArgumentError.notNull([String name]) | |
| 209 : this.value(null, name, "Must not be null"); | |
| 166 | 210 |
| 167 String toString() { | 211 String toString() { |
| 168 if (message != null) { | 212 if (!_hasValue) { |
| 169 return "Illegal argument(s): $message"; | 213 if (message != null) { |
| 214 return "Invalid argument(s): $message"; | |
| 215 } | |
| 216 return "Invalid argument(s)"; | |
| 170 } | 217 } |
| 171 return "Illegal argument(s)"; | 218 String nameString = ""; |
| 219 if (name != null) { | |
| 220 nameString = " ($name)"; | |
| 221 } | |
| 222 return "$message$nameString: ${Error.safeToString(invalidValue)}"; | |
| 172 } | 223 } |
| 173 } | 224 } |
| 174 | 225 |
| 175 /** | 226 /** |
| 176 * Error thrown due to an index being outside a valid range. | 227 * Error thrown due to an index being outside a valid range. |
| 177 */ | 228 */ |
| 178 class RangeError extends ArgumentError { | 229 class RangeError extends ArgumentError { |
| 230 /** The value that is outside its valid range. */ | |
| 231 final num invalidValue; | |
| 232 /** The minimum value that [value] is allowed to assume. */ | |
| 233 final num start; | |
| 234 /** The maximum value that [value] is allowed to assume. */ | |
| 235 final num end; | |
| 236 | |
| 179 // TODO(lrn): This constructor should be called only with string values. | 237 // TODO(lrn): This constructor should be called only with string values. |
| 180 // It currently isn't in all cases. | 238 // It currently isn't in all cases. |
| 181 /** | 239 /** |
| 182 * Create a new [RangeError] with the given [message]. | 240 * Create a new [RangeError] with the given [message]. |
| 183 */ | 241 */ |
| 184 RangeError(var message) : super(message); | 242 RangeError(var message) |
| 243 : invalidValue = null, start = null, end = null, super(message); | |
| 185 | 244 |
| 186 /** Create a new [RangeError] with a message for the given [value]. */ | 245 /** Create a new [RangeError] with a message for the given [value]. */ |
| 187 RangeError.value(num value) : super("value $value"); | 246 RangeError.value(num value, [String message = "Value not in range"]) |
| 247 : invalidValue = value, start = null, end = null, | |
| 248 super(message); | |
| 188 | 249 |
| 189 /** | 250 /** |
| 190 * Create a new [RangeError] with a message for a value and a range. | 251 * Create a new [RangeError] with for an invalid value being outside a range. |
| 191 * | 252 * |
| 192 * The allowed range is from [start] to [end], inclusive. | 253 * The allowed range is from [start] to [end], inclusive. |
| 254 * If `start` or `end` are `null`, the range is infinite in that direction. | |
| 255 * | |
| 256 * For a range from 0 to the length of something, end exclusive, use | |
| 257 * [RangeError.index]. | |
| 193 */ | 258 */ |
| 194 RangeError.range(num value, num start, num end) | 259 RangeError.range(this.invalidValue, this.start, this.end, |
| 195 : super("value $value not in range $start..$end"); | 260 [String message = "Invalid value"]) : super(message); |
| 196 | 261 |
| 197 String toString() => "RangeError: $message"; | 262 /** |
| 263 * Creates a new [RangeError] stating that [index] is not a valid index | |
| 264 * into [indexable]. | |
| 265 * | |
| 266 * The [length] is the length of [indexable] at the time of the error. | |
| 267 * If `length` is omitted, it defaults to `indexable.length`. | |
| 268 * | |
| 269 * The message is used as part of the string representation of the error. | |
| 270 */ | |
| 271 factory RangeError.index(int index, indexable, | |
| 272 [String message, | |
| 273 int length]) = IndexError; | |
|
sra1
2014/11/11 22:04:11
Doesn't this forwarding incorrectly store the inde
Lasse Reichstein Nielsen
2014/11/12 07:38:06
Yes, fixed in follow-up CL.
Only Dart2js actually
| |
| 274 | |
| 275 String toString() { | |
| 276 if (invalidValue == null) return "$message"; | |
| 277 String value = Error.safeToString(invalidValue); | |
| 278 if (start == null) { | |
| 279 if (end == null) { | |
| 280 return "$message ($value)"; | |
| 281 } | |
| 282 return "$message ($value): Value must be less than or equal to $end"; | |
| 283 } | |
| 284 if (end == null) { | |
| 285 return "$message ($value): Value must be greater than or equal to $start"; | |
| 286 } | |
| 287 if (end > start) { | |
| 288 return "$message ($value): Value must be in range $start..$end, " | |
| 289 "inclusive."; | |
| 290 } | |
| 291 if (end < start) return "$message ($value): Valid range is empty"; | |
| 292 return "$message ($value): Only valid value is $start"; | |
| 293 } | |
| 294 } | |
| 295 | |
| 296 /** | |
| 297 * A specialized [RangeError] used when an index is not in the range | |
| 298 * `0..indexable.length-1`. | |
| 299 * | |
| 300 * Also contains the indexable object, its length at the time of the error, | |
| 301 * and the invalid index itself. | |
| 302 */ | |
| 303 class IndexError extends ArgumentError implements RangeError { | |
| 304 /** The indexable object that [index] was not a valid index into. */ | |
| 305 final indexable; | |
| 306 /** The invalid index. */ | |
| 307 final int invalidValue; | |
|
sra1
2014/11/11 22:04:11
This field shadows the field of the same name in A
Lasse Reichstein Nielsen
2014/11/12 07:38:06
Yes, this is silly. Using implements ArgumentError
| |
| 308 /** The length of [indexable] at the time of the error. */ | |
| 309 final int length; | |
| 310 | |
| 311 /** | |
| 312 * Creates a new [IndexError] stating that [invalidValue] is not a valid index | |
| 313 * into [indexable]. | |
| 314 * | |
| 315 * The [length] is the length of [indexable] at the time of the error. | |
| 316 * If `length` is omitted, it defaults to `indexable.length`. | |
| 317 * | |
| 318 * The message is used as part of the string representation of the error. | |
| 319 */ | |
| 320 IndexError(indexable, this.invalidValue, | |
| 321 [String message = "Index out of range", int length]) | |
| 322 : this.indexable = indexable, | |
| 323 this.length = (length != null) ? length : indexable.length, | |
| 324 super(message); | |
| 325 | |
| 326 // Getters inherited from RangeError. | |
| 327 int get start => 0; | |
| 328 int get end => length - 1; | |
| 329 | |
| 330 String toString() { | |
| 331 String target = Error.safeToString(indexable); | |
| 332 if (invalidValue < 0) { | |
| 333 return "RangeError: $message ($target[$invalidValue]): " | |
| 334 "index must not be negative."; | |
| 335 } | |
| 336 return "RangeError: $message: ($target[$invalidValue]): " | |
| 337 "index should be less than $length."; | |
| 338 } | |
| 198 } | 339 } |
| 199 | 340 |
| 200 | 341 |
| 201 /** | 342 /** |
| 202 * Error thrown when control reaches the end of a switch case. | 343 * Error thrown when control reaches the end of a switch case. |
| 203 * | 344 * |
| 204 * The Dart specification requires this error to be thrown when | 345 * The Dart specification requires this error to be thrown when |
| 205 * control reaches the end of a switch case (except the last case | 346 * control reaches the end of a switch case (except the last case |
| 206 * of a switch) without meeting a break or similar end of the control | 347 * of a switch) without meeting a break or similar end of the control |
| 207 * flow. | 348 * flow. |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 * the first time it is read. If evaluating the initializer expression causes | 499 * the first time it is read. If evaluating the initializer expression causes |
| 359 * another read of the variable, this error is thrown. | 500 * another read of the variable, this error is thrown. |
| 360 */ | 501 */ |
| 361 class CyclicInitializationError extends Error { | 502 class CyclicInitializationError extends Error { |
| 362 final String variableName; | 503 final String variableName; |
| 363 CyclicInitializationError([this.variableName]); | 504 CyclicInitializationError([this.variableName]); |
| 364 String toString() => variableName == null | 505 String toString() => variableName == null |
| 365 ? "Reading static variable during its initialization" | 506 ? "Reading static variable during its initialization" |
| 366 : "Reading static variable '$variableName' during its initialization"; | 507 : "Reading static variable '$variableName' during its initialization"; |
| 367 } | 508 } |
| OLD | NEW |