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

Unified Diff: sdk/lib/convert/json.dart

Issue 3003373002: Improve error message for JSON encoding. (Closed)
Patch Set: Created 3 years, 4 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 | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/convert/json.dart
diff --git a/sdk/lib/convert/json.dart b/sdk/lib/convert/json.dart
index 6997b3d08cd42365cfdd6fdce8095bd52b596965..73138cead26d60a51bd11c2de4ab644550c58c7f 100644
--- a/sdk/lib/convert/json.dart
+++ b/sdk/lib/convert/json.dart
@@ -15,19 +15,29 @@ part of dart.convert;
* serializable, the [cause] is null.
*/
class JsonUnsupportedObjectError extends Error {
- /** The object that could not be serialized. */
- final unsupportedObject;
- /** The exception thrown when trying to convert the object. */
- final cause;
+ /// The object that could not be serialized.
+ final Object unsupportedObject;
- JsonUnsupportedObjectError(this.unsupportedObject, {this.cause});
+ /// The exception thrown when trying to convert the object.
+ final Object cause;
+
+ /// The partial result of the conversion, up until the error happened.
+ ///
+ /// May be null.
+ final String partialResult;
+
+ JsonUnsupportedObjectError(this.unsupportedObject,
+ {this.cause, this.partialResult});
String toString() {
+ String safeString = Error.safeToString(unsupportedObject);
+ String prefix;
if (cause != null) {
- return "Converting object to an encodable object failed.";
+ prefix = "Converting object to an encodable object failed:";
} else {
- return "Converting object did not return an encodable object.";
+ prefix = "Converting object did not return an encodable object:";
}
+ return "$prefix $safeString";
}
}
@@ -542,6 +552,8 @@ abstract class _JsonStringifier {
_JsonStringifier(toEncodable(o))
: _toEncodable = toEncodable ?? _defaultToEncodable;
+ String get _partialResult;
+
/** Append a string to the JSON output. */
void writeString(String characters);
/** Append part of a string to the JSON output. */
@@ -647,11 +659,13 @@ abstract class _JsonStringifier {
try {
var customJson = _toEncodable(object);
if (!writeJsonValue(customJson)) {
- throw new JsonUnsupportedObjectError(object);
+ throw new JsonUnsupportedObjectError(object,
+ partialResult: _partialResult);
}
_removeSeen(object);
} catch (e) {
- throw new JsonUnsupportedObjectError(object, cause: e);
+ throw new JsonUnsupportedObjectError(object,
+ cause: e, partialResult: _partialResult);
}
}
@@ -852,6 +866,8 @@ class _JsonStringStringifier extends _JsonStringifier {
stringifier.writeObject(object);
}
+ String get _partialResult => _sink is StringBuffer ? _sink.toString() : null;
+
void writeNumber(num number) {
_sink.write(number.toString());
}
@@ -936,6 +952,8 @@ class _JsonUtf8Stringifier extends _JsonStringifier {
index = 0;
}
+ String get _partialResult => null;
+
void writeNumber(num number) {
writeAsciiString(number.toString());
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698