Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions json_annotation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.2.6

* `CheckedFromJsonException`
* The constructor is now public.
* The `message` property is now `String` (instead of `Object`).

* Added `JsonKey.defaultValue`.

* Added helpers for deserialization of `enum` values.
Expand Down
32 changes: 24 additions & 8 deletions json_annotation/lib/src/checked_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ T $checkedNew<T>(String className, Map map, T constructor(),
if (error is ArgumentError) {
key = fieldKeyMap[error.name] ?? error.name;
}
throw new CheckedFromJsonException._(error, stack, map, key,
throw new CheckedFromJsonException(error, stack, map, key,
className: className);
}
}
Expand All @@ -38,28 +38,44 @@ T $checkedConvert<T>(Map map, String key, T castFunc(Object value)) {
} on CheckedFromJsonException {
rethrow;
} catch (error, stack) {
throw new CheckedFromJsonException._(error, stack, map, key);
throw new CheckedFromJsonException(error, stack, map, key);
}
}

/// Exception thrown if there is a runtime exception in `fromJson`
/// code generated when `JsonSerializableGenerator.checked` is `true`
class CheckedFromJsonException implements Exception {
/// The [Error] or [Exception] that triggered this exception.
final Object innerError;

/// The [StackTrace] for the [Error] or [Exception] that triggered this
/// exception.
final StackTrace innerStack;

/// The key from [map] that corresponds to the thrown [innerError].
///
/// May be `null`.
final String key;

/// The source [Map] that was used for decoding when the [innerError] was
/// thrown.
final Map map;
final Object message;

String _className;
/// A human-readable message corresponding to [innerError].
///
/// May be `null`.
final String message;

/// The name of the class being created when [innerError] was thrown.
String get className => _className;
String _className;

CheckedFromJsonException._(
this.innerError, this.innerStack, this.map, this.key,
/// Creates a new instance of [CheckedFromJsonException].
CheckedFromJsonException(this.innerError, this.innerStack, this.map, this.key,
{String className})
: _className = className,
message = _getMessage(innerError);

static Object _getMessage(Object error) =>
(error is ArgumentError) ? error.message : null;
static String _getMessage(Object error) =>
(error is ArgumentError) ? error.message?.toString() : null;
}