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

Unified Diff: sdk/lib/core/errors.dart

Issue 879893002: Make RangeError.toString use the name field that it inherits from ArgumentError. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add tests. Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/corelib/range_error_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/errors.dart
diff --git a/sdk/lib/core/errors.dart b/sdk/lib/core/errors.dart
index 0ac18808e643191f51e96665a57656df8ad4e2bf..0244ea91753d4e98bf7f9d9ff093520e27a22b5d 100644
--- a/sdk/lib/core/errors.dart
+++ b/sdk/lib/core/errors.dart
@@ -157,7 +157,7 @@ class ArgumentError extends Error {
*/
ArgumentError.value(value,
[String this.name,
- String this.message = "Invalid argument"])
+ String this.message])
: invalidValue = value,
_hasValue = true;
@@ -170,19 +170,22 @@ class ArgumentError extends Error {
ArgumentError.notNull([String name])
: this.value(null, name, "Must not be null");
+ // Helper functions for toString overridden in subclasses.
+ String get _errorName => "Invalid argument${name == null ? "(s)" : ""}";
+ String get _errorExplanation => "";
+
String toString() {
- if (!_hasValue) {
- var result = "Invalid arguments(s)";
- if (message != null) {
- result = "$result: $message";
- }
- return result;
- }
String nameString = "";
if (name != null) {
nameString = " ($name)";
}
- return "$message$nameString: ${Error.safeToString(invalidValue)}";
+ var message = this.message == null ? "" : ": ${this.message}";
+ String prefix = "$_errorName$nameString$message";
+ if (invalidValue == null) return prefix;
+ // If we know the invalid value, we can try to describe the problem.
+ String explanation = _errorExplanation;
+ String errorValue = Error.safeToString(invalidValue);
+ return "$prefix$explanation: $errorValue";
}
}
@@ -274,7 +277,7 @@ class RangeError extends ArgumentError {
* `[]` that accepts an index if `0 <= index < length`.
*
* If [length] is provided, it is used as the length of the indexable object,
- * otherwise the length is found as `idexable.length`.
+ * otherwise the length is found as `indexable.length`.
*/
static void checkValidIndex(int index, var indexable,
[String name, int length, String message]) {
@@ -320,9 +323,9 @@ class RangeError extends ArgumentError {
if (value < 0) throw new RangeError.range(value, 0, null, name, message);
}
- String toString() {
- if (!_hasValue) return "RangeError: $message";
- String value = Error.safeToString(invalidValue);
+ String get _errorName => "RangeError";
+ String get _errorExplanation {
+ assert(_hasValue);
String explanation = "";
if (start == null) {
if (end != null) {
@@ -332,14 +335,14 @@ class RangeError extends ArgumentError {
} else if (end == null) {
explanation = ": Not greater than or equal to $start";
} else if (end > start) {
- explanation = ": Not in range $start..$end, inclusive.";
+ explanation = ": Not in range $start..$end, inclusive";
} else if (end < start) {
explanation = ": Valid value range is empty";
} else {
// end == start.
explanation = ": Only valid value is $start";
}
- return "RangeError: $message ($value)$explanation";
+ return explanation;
}
}
@@ -376,14 +379,15 @@ class IndexError extends ArgumentError implements RangeError {
int get start => 0;
int get end => length - 1;
- String toString() {
+ String get _errorName => "RangeError";
+ String get _errorExplanation {
assert(_hasValue);
String target = Error.safeToString(indexable);
- var explanation = "index should be less than $length";
+ var explanation = ": index should be less than $length";
if (invalidValue < 0) {
- explanation = "index must not be negative";
+ explanation = ": index must not be negative";
}
- return "RangeError: $message ($target[$invalidValue]): $explanation";
+ return explanation;
}
}
« no previous file with comments | « no previous file | tests/corelib/range_error_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698