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 } | 7 } |
8 | 8 |
9 /** | 9 /** |
10 * Error thrown by the runtime system when an assert statement fails. | 10 * Error thrown by the runtime system when an assert statement fails. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 final String _className; | 66 final String _className; |
67 const AbstractClassInstantiationError(String this._className); | 67 const AbstractClassInstantiationError(String this._className); |
68 String toString() => "Cannot instantiate abstract class: '$_className'"; | 68 String toString() => "Cannot instantiate abstract class: '$_className'"; |
69 } | 69 } |
70 | 70 |
71 /** | 71 /** |
72 * Error thrown by the default implementation of [:noSuchMethod:] on [Object]. | 72 * Error thrown by the default implementation of [:noSuchMethod:] on [Object]. |
73 */ | 73 */ |
74 class NoSuchMethodError implements Error { | 74 class NoSuchMethodError implements Error { |
75 final Object _receiver; | 75 final Object _receiver; |
76 final String _functionName; | 76 final String _memberName; |
77 final List _arguments; | 77 final List _arguments; |
| 78 final Map<String,Dynamic> _namedArguments; |
78 final List _existingArgumentNames; | 79 final List _existingArgumentNames; |
79 | 80 |
80 /** | 81 /** |
81 * Create a [NoSuchMethodError] corresponding to a failed method call. | 82 * Create a [NoSuchMethodError] corresponding to a failed method call. |
82 * | 83 * |
83 * The first parameter is the receiver of the method call. | 84 * The first parameter to this constructor is the receiver of the method call. |
84 * The second parameter is the name of the called method. | 85 * That is, the object on which the method was attempted called. |
85 * The third parameter is the positional arguments that the method was | 86 * The second parameter is the name of the called method or accessor. |
86 * called with. | 87 * The third parameter is a list of the positional arguments that the method |
| 88 * was called with. |
| 89 * The fourth parameter is a map from [String] names to the values of named |
| 90 * arguments that the method was called with. |
87 * The optional [exisitingArgumentNames] is the expected parameters of a | 91 * The optional [exisitingArgumentNames] is the expected parameters of a |
88 * method with the same name on the receiver, if available. This is | 92 * method with the same name on the receiver, if available. This is |
89 * the method that would have been called if the parameters had matched. | 93 * the method that would have been called if the parameters had matched. |
90 * | |
91 * TODO(lrn): This will be rewritten to use mirrors when they are available. | |
92 */ | 94 */ |
93 const NoSuchMethodError(Object this._receiver, | 95 const NoSuchMethodError(Object this._receiver, |
94 String this._functionName, | 96 String this._memberName, |
95 List this._arguments, | 97 List this._arguments, |
| 98 Map<String,Dynamic> this._namedArguments, |
96 [List existingArgumentNames = null]) | 99 [List existingArgumentNames = null]) |
97 : this._existingArgumentNames = existingArgumentNames; | 100 : this._existingArgumentNames = existingArgumentNames; |
98 | 101 |
99 String toString() { | 102 String toString() { |
100 StringBuffer sb = new StringBuffer(); | 103 StringBuffer sb = new StringBuffer(); |
101 for (int i = 0; i < _arguments.length; i++) { | 104 int i = 0; |
102 if (i > 0) { | 105 if (_arguments != null) { |
103 sb.add(", "); | 106 for (; i < _arguments.length; i++) { |
| 107 if (i > 0) { |
| 108 sb.add(", "); |
| 109 } |
| 110 sb.add(safeToString(_arguments[i])); |
104 } | 111 } |
105 sb.add(safeToString(_arguments[i])); | 112 } |
| 113 if (_namedArguments != null) { |
| 114 _namedArguments.forEach((String key, var value) { |
| 115 if (i > 0) { |
| 116 sb.add(", "); |
| 117 } |
| 118 sb.add(key); |
| 119 sb.add(": "); |
| 120 sb.add(safeToString(value)); |
| 121 i++; |
| 122 }); |
106 } | 123 } |
107 if (_existingArgumentNames === null) { | 124 if (_existingArgumentNames === null) { |
108 return "NoSuchMethodError : method not found: '$_functionName'\n" | 125 return "NoSuchMethodError : method not found: '$_memberName'\n" |
109 "Receiver: ${safeToString(_receiver)}\n" | 126 "Receiver: ${safeToString(_receiver)}\n" |
110 "Arguments: [$sb]"; | 127 "Arguments: [$sb]"; |
111 } else { | 128 } else { |
112 String actualParameters = sb.toString(); | 129 String actualParameters = sb.toString(); |
113 sb = new StringBuffer(); | 130 sb = new StringBuffer(); |
114 for (int i = 0; i < _existingArgumentNames.length; i++) { | 131 for (int i = 0; i < _existingArgumentNames.length; i++) { |
115 if (i > 0) { | 132 if (i > 0) { |
116 sb.add(", "); | 133 sb.add(", "); |
117 } | 134 } |
118 sb.add(_existingArgumentNames[i]); | 135 sb.add(_existingArgumentNames[i]); |
119 } | 136 } |
120 String formalParameters = sb.toString(); | 137 String formalParameters = sb.toString(); |
121 return "NoSuchMethodError: incorrect number of arguments passed to " | 138 return "NoSuchMethodError: incorrect number of arguments passed to " |
122 "method named '$_functionName'\n" | 139 "method named '$_memberName'\n" |
123 "Receiver: ${safeToString(_receiver)}\n" | 140 "Receiver: ${safeToString(_receiver)}\n" |
124 "Tried calling: $_functionName($actualParameters)\n" | 141 "Tried calling: $_memberName($actualParameters)\n" |
125 "Found: $_functionName($formalParameters)"; | 142 "Found: $_memberName($formalParameters)"; |
126 } | 143 } |
127 } | 144 } |
128 | 145 |
129 static String safeToString(Object object) { | 146 static String safeToString(Object object) { |
130 if (object is int || object is double || object is bool || null == object) { | 147 if (object is int || object is double || object is bool || null == object) { |
131 return object.toString(); | 148 return object.toString(); |
132 } | 149 } |
133 if (object is String) { | 150 if (object is String) { |
134 // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed. | 151 // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed. |
135 const backslash = '\\'; | 152 const backslash = '\\'; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 191 |
175 class OutOfMemoryError implements Error { | 192 class OutOfMemoryError implements Error { |
176 const OutOfMemoryError(); | 193 const OutOfMemoryError(); |
177 String toString() => "Out of Memory"; | 194 String toString() => "Out of Memory"; |
178 } | 195 } |
179 | 196 |
180 class StackOverflowError implements Error { | 197 class StackOverflowError implements Error { |
181 const StackOverflowError(); | 198 const StackOverflowError(); |
182 String toString() => "Stack Overflow"; | 199 String toString() => "Stack Overflow"; |
183 } | 200 } |
OLD | NEW |