| 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 class Error { | 7 class Error { |
| 8 const Error(); |
| 9 |
| 8 /** | 10 /** |
| 9 * Safely convert a value to a [String] description. | 11 * Safely convert a value to a [String] description. |
| 10 * | 12 * |
| 11 * The conversion is guaranteed to not throw, so it won't use the object's | 13 * The conversion is guaranteed to not throw, so it won't use the object's |
| 12 * toString method. | 14 * toString method. |
| 13 */ | 15 */ |
| 14 static String safeToString(Object object) { | 16 static String safeToString(Object object) { |
| 15 if (object is int || object is double || object is bool || null == object) { | 17 if (object is int || object is double || object is bool || null == object) { |
| 16 return object.toString(); | 18 return object.toString(); |
| 17 } | 19 } |
| 18 if (object is String) { | 20 if (object is String) { |
| 19 // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed. | 21 // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed. |
| 20 String string = object; | 22 String string = object; |
| 21 const backslash = '\\'; | 23 const backslash = '\\'; |
| 22 String escaped = string | 24 String escaped = string |
| 23 .replaceAll('$backslash', '$backslash$backslash') | 25 .replaceAll('$backslash', '$backslash$backslash') |
| 24 .replaceAll('\n', '${backslash}n') | 26 .replaceAll('\n', '${backslash}n') |
| 25 .replaceAll('\r', '${backslash}r') | 27 .replaceAll('\r', '${backslash}r') |
| 26 .replaceAll('"', '$backslash"'); | 28 .replaceAll('"', '$backslash"'); |
| 27 return '"$escaped"'; | 29 return '"$escaped"'; |
| 28 } | 30 } |
| 29 return _objectToString(object); | 31 return _objectToString(object); |
| 30 } | 32 } |
| 31 | 33 |
| 32 external static String _objectToString(Object object); | 34 external static String _objectToString(Object object); |
| 33 | 35 |
| 36 /** |
| 37 * The stack trace of `this` error. May be null. |
| 38 */ |
| 39 StackTrace get stackTrace => null; |
| 40 } |
| 41 |
| 42 /** |
| 43 * Captures the stack trace when thrown. |
| 44 */ |
| 45 class StackTraceOnThrow { |
| 46 /** |
| 47 * Captures the current stack-trace when thrown. |
| 48 * |
| 49 * Classes extending or mixing-in this class will automatically have a stack |
| 50 * trace captured the first time they are thrown. This stack trace is returned |
| 51 * by the [stackTrace] getter. |
| 52 * |
| 53 * Throwing the object again will not cause overwrite the first stack trace, |
| 54 * so bjects that are `StackTraceOnThrow` instances should not be reused. |
| 55 */ |
| 34 external StackTrace get stackTrace; | 56 external StackTrace get stackTrace; |
| 35 } | 57 } |
| 36 | 58 |
| 37 /** | 59 /** |
| 38 * Error thrown by the runtime system when an assert statement fails. | 60 * Error thrown by the runtime system when an assert statement fails. |
| 39 */ | 61 */ |
| 40 class AssertionError extends Error { | 62 class AssertionError extends Error with StackTraceOnThrow { |
| 41 } | 63 } |
| 42 | 64 |
| 43 /** | 65 /** |
| 44 * Error thrown by the runtime system when a type assertion fails. | 66 * Error thrown by the runtime system when a type assertion fails. |
| 45 */ | 67 */ |
| 46 class TypeError extends AssertionError { | 68 class TypeError extends AssertionError { |
| 47 } | 69 } |
| 48 | 70 |
| 49 /** | 71 /** |
| 50 * Error thrown by the runtime system when a cast operation fails. | 72 * Error thrown by the runtime system when a cast operation fails. |
| 51 */ | 73 */ |
| 52 class CastError extends Error { | 74 class CastError extends Error with StackTraceOnThrow { |
| 53 } | 75 } |
| 54 | 76 |
| 55 /** | 77 /** |
| 56 * Error thrown when attempting to throw [:null:]. | 78 * Error thrown when attempting to throw [:null:]. |
| 57 */ | 79 */ |
| 58 class NullThrownError extends Error { | 80 class NullThrownError extends Error with StackTraceOnThrow { |
| 59 NullThrownError(); | 81 NullThrownError(); |
| 60 String toString() => "Throw of null."; | 82 String toString() => "Throw of null."; |
| 61 } | 83 } |
| 62 | 84 |
| 63 /** | 85 /** |
| 64 * Error thrown when a function is passed an unacceptable argument. | 86 * Error thrown when a function is passed an unacceptable argument. |
| 65 */ | 87 */ |
| 66 class ArgumentError extends Error { | 88 class ArgumentError extends Error with StackTraceOnThrow { |
| 67 final message; | 89 final message; |
| 68 | 90 |
| 69 /** The [message] describes the erroneous argument. */ | 91 /** The [message] describes the erroneous argument. */ |
| 70 ArgumentError([this.message]); | 92 ArgumentError([this.message]); |
| 71 | 93 |
| 72 String toString() { | 94 String toString() { |
| 73 if (message != null) { | 95 if (message != null) { |
| 74 return "Illegal argument(s): $message"; | 96 return "Illegal argument(s): $message"; |
| 75 } | 97 } |
| 76 return "Illegal argument(s)"; | 98 return "Illegal argument(s)"; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 103 | 125 |
| 104 | 126 |
| 105 /** | 127 /** |
| 106 * Error thrown when control reaches the end of a switch case. | 128 * Error thrown when control reaches the end of a switch case. |
| 107 * | 129 * |
| 108 * The Dart specification requires this error to be thrown when | 130 * The Dart specification requires this error to be thrown when |
| 109 * control reaches the end of a switch case (except the last case | 131 * control reaches the end of a switch case (except the last case |
| 110 * of a switch) without meeting a break or similar end of the control | 132 * of a switch) without meeting a break or similar end of the control |
| 111 * flow. | 133 * flow. |
| 112 */ | 134 */ |
| 113 class FallThroughError extends Error { | 135 class FallThroughError extends Error with StackTraceOnThrow { |
| 114 FallThroughError(); | |
| 115 } | 136 } |
| 116 | 137 |
| 117 | 138 |
| 118 class AbstractClassInstantiationError extends Error { | 139 class AbstractClassInstantiationError |
| 140 extends Error with StackTraceOnThrow { |
| 119 final String _className; | 141 final String _className; |
| 120 AbstractClassInstantiationError(String this._className); | 142 AbstractClassInstantiationError(String this._className); |
| 121 String toString() => "Cannot instantiate abstract class: '$_className'"; | 143 String toString() => "Cannot instantiate abstract class: '$_className'"; |
| 122 } | 144 } |
| 123 | 145 |
| 124 /** | 146 /** |
| 125 * Error thrown by the default implementation of [:noSuchMethod:] on [Object]. | 147 * Error thrown by the default implementation of [:noSuchMethod:] on [Object]. |
| 126 */ | 148 */ |
| 127 class NoSuchMethodError extends Error { | 149 class NoSuchMethodError extends Error with StackTraceOnThrow { |
| 128 final Object _receiver; | 150 final Object _receiver; |
| 129 final String _memberName; | 151 final String _memberName; |
| 130 final List _arguments; | 152 final List _arguments; |
| 131 final Map<String,dynamic> _namedArguments; | 153 final Map<String,dynamic> _namedArguments; |
| 132 final List _existingArgumentNames; | 154 final List _existingArgumentNames; |
| 133 | 155 |
| 134 /** | 156 /** |
| 135 * Create a [NoSuchMethodError] corresponding to a failed method call. | 157 * Create a [NoSuchMethodError] corresponding to a failed method call. |
| 136 * | 158 * |
| 137 * The first parameter to this constructor is the receiver of the method call. | 159 * The first parameter to this constructor is the receiver of the method call. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 155 external String toString(); | 177 external String toString(); |
| 156 } | 178 } |
| 157 | 179 |
| 158 | 180 |
| 159 /** | 181 /** |
| 160 * The operation was not allowed by the object. | 182 * The operation was not allowed by the object. |
| 161 * | 183 * |
| 162 * This [Error] is thrown when an instance cannot implement one of the methods | 184 * This [Error] is thrown when an instance cannot implement one of the methods |
| 163 * in its signature. | 185 * in its signature. |
| 164 */ | 186 */ |
| 165 class UnsupportedError extends Error { | 187 class UnsupportedError extends Error with StackTraceOnThrow { |
| 166 final String message; | 188 final String message; |
| 167 UnsupportedError(this.message); | 189 UnsupportedError(this.message); |
| 168 String toString() => "Unsupported operation: $message"; | 190 String toString() => "Unsupported operation: $message"; |
| 169 } | 191 } |
| 170 | 192 |
| 171 | 193 |
| 172 /** | 194 /** |
| 173 * Thrown by operations that have not been implemented yet. | 195 * Thrown by operations that have not been implemented yet. |
| 174 * | 196 * |
| 175 * This [Error] is thrown by unfinished code that hasn't yet implemented | 197 * This [Error] is thrown by unfinished code that hasn't yet implemented |
| 176 * all the features it needs. | 198 * all the features it needs. |
| 177 * | 199 * |
| 178 * If a class is not intending to implement the feature, it should throw | 200 * If a class is not intending to implement the feature, it should throw |
| 179 * an [UnsupportedError] instead. This error is only intended for | 201 * an [UnsupportedError] instead. This error is only intended for |
| 180 * use during development. | 202 * use during development. |
| 181 */ | 203 */ |
| 182 class UnimplementedError extends Error implements UnsupportedError { | 204 class UnimplementedError |
| 205 extends Error with StackTraceOnThrow implements UnsupportedError { |
| 183 final String message; | 206 final String message; |
| 184 UnimplementedError([String this.message]); | 207 UnimplementedError([String this.message]); |
| 185 String toString() => (this.message != null | 208 String toString() => (this.message != null |
| 186 ? "UnimplementedError: $message" | 209 ? "UnimplementedError: $message" |
| 187 : "UnimplementedError"); | 210 : "UnimplementedError"); |
| 188 } | 211 } |
| 189 | 212 |
| 190 | 213 |
| 191 /** | 214 /** |
| 192 * The operation was not allowed by the current state of the object. | 215 * The operation was not allowed by the current state of the object. |
| 193 * | 216 * |
| 194 * This is a generic error used for a variety of different erroneous | 217 * This is a generic error used for a variety of different erroneous |
| 195 * actions. The message should be descriptive. | 218 * actions. The message should be descriptive. |
| 196 */ | 219 */ |
| 197 class StateError extends Error { | 220 class StateError extends Error with StackTraceOnThrow { |
| 198 final String message; | 221 final String message; |
| 199 StateError(this.message); | 222 StateError(this.message); |
| 200 String toString() => "Bad state: $message"; | 223 String toString() => "Bad state: $message"; |
| 201 } | 224 } |
| 202 | 225 |
| 203 | 226 |
| 204 /** | 227 /** |
| 205 * Error occurring when a collection is modified during iteration. | 228 * Error occurring when a collection is modified during iteration. |
| 206 * | 229 * |
| 207 * Some modifications may be allowed for some collections, so each collection | 230 * Some modifications may be allowed for some collections, so each collection |
| 208 * ([Iterable] or similar collection of values) should declare which operations | 231 * ([Iterable] or similar collection of values) should declare which operations |
| 209 * are allowed during an iteration. | 232 * are allowed during an iteration. |
| 210 */ | 233 */ |
| 211 class ConcurrentModificationError extends Error { | 234 class ConcurrentModificationError extends Error with StackTraceOnThrow { |
| 212 /** The object that was modified in an incompatible way. */ | 235 /** The object that was modified in an incompatible way. */ |
| 213 final Object modifiedObject; | 236 final Object modifiedObject; |
| 214 | 237 |
| 215 ConcurrentModificationError([this.modifiedObject]); | 238 ConcurrentModificationError([this.modifiedObject]); |
| 216 | 239 |
| 217 String toString() { | 240 String toString() { |
| 218 if (modifiedObject == null) { | 241 if (modifiedObject == null) { |
| 219 return "Concurrent modification during iteration."; | 242 return "Concurrent modification during iteration."; |
| 220 } | 243 } |
| 221 return "Concurrent modification during iteration: " | 244 return "Concurrent modification during iteration: " |
| 222 "${Error.safeToString(modifiedObject)}."; | 245 "${Error.safeToString(modifiedObject)}."; |
| 223 } | 246 } |
| 224 } | 247 } |
| 225 | 248 |
| 226 | 249 |
| 227 class OutOfMemoryError implements Error { | 250 class OutOfMemoryError extends Error { |
| 228 const OutOfMemoryError(); | 251 const OutOfMemoryError(); |
| 229 String toString() => "Out of Memory"; | 252 String toString() => "Out of Memory"; |
| 230 | |
| 231 StackTrace get stackTrace => null; | |
| 232 } | 253 } |
| 233 | 254 |
| 234 | 255 |
| 235 class StackOverflowError implements Error { | 256 class StackOverflowError extends Error { |
| 236 const StackOverflowError(); | 257 const StackOverflowError(); |
| 237 String toString() => "Stack Overflow"; | 258 String toString() => "Stack Overflow"; |
| 238 | |
| 239 StackTrace get stackTrace => null; | |
| 240 } | 259 } |
| 241 | 260 |
| 242 /** | 261 /** |
| 243 * Error thrown when a lazily initialized variable cannot be initialized. | 262 * Error thrown when a lazily initialized variable cannot be initialized. |
| 244 * | 263 * |
| 245 * A static/library variable with an initializer expression is initialized | 264 * A static/library variable with an initializer expression is initialized |
| 246 * the first time it is read. If evaluating the initializer expression causes | 265 * the first time it is read. If evaluating the initializer expression causes |
| 247 * another read of the variable, this error is thrown. | 266 * another read of the variable, this error is thrown. |
| 248 */ | 267 */ |
| 249 class CyclicInitializationError extends Error { | 268 class CyclicInitializationError extends Error with StackTraceOnThrow { |
| 250 final String variableName; | 269 final String variableName; |
| 251 CyclicInitializationError([this.variableName]); | 270 CyclicInitializationError([this.variableName]); |
| 252 String toString() => variableName == null | 271 String toString() => variableName == null |
| 253 ? "Reading static variable during its initialization" | 272 ? "Reading static variable during its initialization" |
| 254 : "Reading static variable '$variableName' during its initialization"; | 273 : "Reading static variable '$variableName' during its initialization"; |
| 255 } | 274 } |
| OLD | NEW |