Skip to content

Commit 6b711e3

Browse files
authored
Improve error messages when typeToCode throws an exception (#1450)
`typeToCode` can throw an expection, which reduces the error message to: ``` UnimplementedError: (InvalidTypeImpl) InvalidType package:json_serializable/src/utils.dart 221:3 typeToCode package:json_serializable/src/helper_core.dart 79:46 createInvalidGenerationError ... ``` This change improves the error message to something like this: ``` Could not generate `fromJson` code for `photos` because of type is unimplemented/unsupported/undefined. package:picthrive_kiosk/ptclient/generated/consolidated_public.swagger.dart:2633:21 ╷ 2633 │ final List<Items> photos; │ ^^^^^^ ╵ ```
1 parent 5d5feca commit 6b711e3

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

json_serializable/lib/src/helper_core.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ $converterOrKeyInstructions
7777
* Set `JsonSerializable.genericArgumentFactories` to `true`
7878
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/genericArgumentFactories.html''';
7979
} else if (field.type != error.type) {
80-
message = '$message because of type `${typeToCode(error.type)}`';
80+
try {
81+
message = '$message because of type `${typeToCode(error.type)}`';
82+
// ignore: avoid_catching_errors
83+
} on UnimplementedError catch (ex) {
84+
message = '$message because type is unimplemented ($ex)';
85+
}
8186
} else {
8287
final element = error.type.element?.name;
8388
todo =

json_serializable/test/json_serializable_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ const _expectedAnnotatedTests = {
153153
'UnsupportedEnum',
154154
'UnsupportedListField',
155155
'UnsupportedMapField',
156+
'UnsupportedNestedFunctionType',
156157
'UnsupportedSetField',
157158
'UnsupportedUriField',
158159
'ValidToFromFuncClassStatic',

json_serializable/test/src/_json_serializable_test_input.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,10 @@ class DateTimeUtcTestClass {
626626

627627
DateTimeUtcTestClass(this.date);
628628
}
629+
630+
@ShouldThrow('''
631+
Could not generate `fromJson` code for `field` because type is unimplemented (UnimplementedError: (FunctionTypeImpl) void Function()).''')
632+
@JsonSerializable(createToJson: false)
633+
class UnsupportedNestedFunctionType {
634+
late List<void Function()> field;
635+
}

0 commit comments

Comments
 (0)