Improve error messages when typeToCode throws an exception#1450
Improve error messages when typeToCode throws an exception#1450fromkeith wants to merge 2 commits intogoogle:masterfrom
Conversation
`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;
│ ^^^^^^
╵
```
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
| } else if (field.type != error.type) { | ||
| message = '$message because of type `${typeToCode(error.type)}`'; | ||
| try { | ||
| message = '$message because of type `${typeToCode(error.type)}`'; |
There was a problem hiding this comment.
We should avoid doing a catch without details. What type is thrown? Unimplemented? From which type?
There was a problem hiding this comment.
Yea, specifically this line is throwing the error
package:json_serializable/src/utils.dart 221:3
throw UnimplementedError('(${type.runtimeType}) $type');
For me type.runtimeType is equal to InvalidTypeImpl when printed in the error.
Would you prefer a refactor like this:
try {
message = '$message because of type `${typeToCode(error.type)}`';
} on UnimplementedError catch (ex) {
message = '$message because type is Unimplemented ($ex)';
}
typeToCodecan throw an expection, which reduces the error message to:This change improves the error message to something like this: