Skip to content

Commit

Permalink
Improve error messages when typeToCode throws an exception
Browse files Browse the repository at this point in the history
`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;
     │                     ^^^^^^
     ╵

```
  • Loading branch information
fromkeith authored Oct 11, 2024
1 parent 3e4dbe3 commit 2802de3
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion json_serializable/lib/src/helper_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ $converterOrKeyInstructions
* Set `JsonSerializable.genericArgumentFactories` to `true`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/genericArgumentFactories.html''';
} else if (field.type != error.type) {
message = '$message because of type `${typeToCode(error.type)}`';
try {
message = '$message because of type `${typeToCode(error.type)}`';
} catch (ex) {
message = '$message because of type is unimplemented/unsupported/undefined';
}
} else {
final element = error.type.element?.name;
todo = '''
Expand Down

0 comments on commit 2802de3

Please sign in to comment.