Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages when typeToCode throws an exception #1450

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fromkeith
Copy link

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;
     │                     ^^^^^^
     ╵

`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;
     │                     ^^^^^^
     ╵

```
Copy link

google-cla bot commented Oct 11, 2024

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.

@@ -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)}`';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid doing a catch without details. What type is thrown? Unimplemented? From which type?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants