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 class Error { | 5 class Error { |
6 const Error(); | 6 const Error(); |
| 7 |
| 8 /** |
| 9 * Safely convert a value to a [String] description. |
| 10 * |
| 11 * The conversion is guaranteed to not throw, so it won't use the object's |
| 12 * toString method. |
| 13 */ |
| 14 static String safeToString(Object object) { |
| 15 if (object is int || object is double || object is bool || null == object) { |
| 16 return object.toString(); |
| 17 } |
| 18 if (object is String) { |
| 19 // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed. |
| 20 const backslash = '\\'; |
| 21 String escaped = object |
| 22 .replaceAll('$backslash', '$backslash$backslash') |
| 23 .replaceAll('\n', '${backslash}n') |
| 24 .replaceAll('\r', '${backslash}r') |
| 25 .replaceAll('"', '$backslash"'); |
| 26 return '"$escaped"'; |
| 27 } |
| 28 return _objectToString(object); |
| 29 } |
| 30 |
| 31 external static String _objectToString(Object object); |
7 } | 32 } |
8 | 33 |
9 /** | 34 /** |
10 * Error thrown by the runtime system when an assert statement fails. | 35 * Error thrown by the runtime system when an assert statement fails. |
11 */ | 36 */ |
12 class AssertionError implements Error { | 37 class AssertionError implements Error { |
13 } | 38 } |
14 | 39 |
15 /** | 40 /** |
16 * Error thrown by the runtime system when a type assertion fails. | 41 * Error thrown by the runtime system when a type assertion fails. |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 : this._existingArgumentNames = existingArgumentNames; | 138 : this._existingArgumentNames = existingArgumentNames; |
114 | 139 |
115 String toString() { | 140 String toString() { |
116 StringBuffer sb = new StringBuffer(); | 141 StringBuffer sb = new StringBuffer(); |
117 int i = 0; | 142 int i = 0; |
118 if (_arguments != null) { | 143 if (_arguments != null) { |
119 for (; i < _arguments.length; i++) { | 144 for (; i < _arguments.length; i++) { |
120 if (i > 0) { | 145 if (i > 0) { |
121 sb.add(", "); | 146 sb.add(", "); |
122 } | 147 } |
123 sb.add(safeToString(_arguments[i])); | 148 sb.add(Error.safeToString(_arguments[i])); |
124 } | 149 } |
125 } | 150 } |
126 if (_namedArguments != null) { | 151 if (_namedArguments != null) { |
127 _namedArguments.forEach((String key, var value) { | 152 _namedArguments.forEach((String key, var value) { |
128 if (i > 0) { | 153 if (i > 0) { |
129 sb.add(", "); | 154 sb.add(", "); |
130 } | 155 } |
131 sb.add(key); | 156 sb.add(key); |
132 sb.add(": "); | 157 sb.add(": "); |
133 sb.add(safeToString(value)); | 158 sb.add(Error.safeToString(value)); |
134 i++; | 159 i++; |
135 }); | 160 }); |
136 } | 161 } |
137 if (_existingArgumentNames == null) { | 162 if (_existingArgumentNames == null) { |
138 return "NoSuchMethodError : method not found: '$_memberName'\n" | 163 return "NoSuchMethodError : method not found: '$_memberName'\n" |
139 "Receiver: ${safeToString(_receiver)}\n" | 164 "Receiver: ${Error.safeToString(_receiver)}\n" |
140 "Arguments: [$sb]"; | 165 "Arguments: [$sb]"; |
141 } else { | 166 } else { |
142 String actualParameters = sb.toString(); | 167 String actualParameters = sb.toString(); |
143 sb = new StringBuffer(); | 168 sb = new StringBuffer(); |
144 for (int i = 0; i < _existingArgumentNames.length; i++) { | 169 for (int i = 0; i < _existingArgumentNames.length; i++) { |
145 if (i > 0) { | 170 if (i > 0) { |
146 sb.add(", "); | 171 sb.add(", "); |
147 } | 172 } |
148 sb.add(_existingArgumentNames[i]); | 173 sb.add(_existingArgumentNames[i]); |
149 } | 174 } |
150 String formalParameters = sb.toString(); | 175 String formalParameters = sb.toString(); |
151 return "NoSuchMethodError: incorrect number of arguments passed to " | 176 return "NoSuchMethodError: incorrect number of arguments passed to " |
152 "method named '$_memberName'\n" | 177 "method named '$_memberName'\n" |
153 "Receiver: ${safeToString(_receiver)}\n" | 178 "Receiver: ${Error.safeToString(_receiver)}\n" |
154 "Tried calling: $_memberName($actualParameters)\n" | 179 "Tried calling: $_memberName($actualParameters)\n" |
155 "Found: $_memberName($formalParameters)"; | 180 "Found: $_memberName($formalParameters)"; |
156 } | 181 } |
157 } | 182 } |
158 | |
159 static String safeToString(Object object) { | |
160 if (object is int || object is double || object is bool || null == object) { | |
161 return object.toString(); | |
162 } | |
163 if (object is String) { | |
164 // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed. | |
165 const backslash = '\\'; | |
166 String escaped = object | |
167 .replaceAll('$backslash', '$backslash$backslash') | |
168 .replaceAll('\n', '${backslash}n') | |
169 .replaceAll('\r', '${backslash}r') | |
170 .replaceAll('"', '$backslash"'); | |
171 return '"$escaped"'; | |
172 } | |
173 return _objectToString(object); | |
174 } | |
175 | |
176 external static String _objectToString(Object object); | |
177 } | 183 } |
178 | 184 |
179 | 185 |
180 /** | 186 /** |
181 * The operation was not allowed by the object. | 187 * The operation was not allowed by the object. |
182 * | 188 * |
183 * This [Error] is thrown when a class cannot implement | 189 * This [Error] is thrown when a class cannot implement |
184 * one of the methods in its signature. | 190 * one of the methods in its signature. |
185 */ | 191 */ |
186 class UnsupportedError implements Error { | 192 class UnsupportedError implements Error { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 | 230 |
225 class OutOfMemoryError implements Error { | 231 class OutOfMemoryError implements Error { |
226 const OutOfMemoryError(); | 232 const OutOfMemoryError(); |
227 String toString() => "Out of Memory"; | 233 String toString() => "Out of Memory"; |
228 } | 234 } |
229 | 235 |
230 class StackOverflowError implements Error { | 236 class StackOverflowError implements Error { |
231 const StackOverflowError(); | 237 const StackOverflowError(); |
232 String toString() => "Stack Overflow"; | 238 String toString() => "Stack Overflow"; |
233 } | 239 } |
OLD | NEW |