Skip to content

Improve error messages when typeToCode throws an exception #1450

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion json_serializable/lib/src/helper_core.dart
Original file line number Diff line number Diff line change
@@ -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)';
}

} on UnimplementedError catch (ex) {
message = '$message because type is Unimplemented ($ex)';
}
} else {
final element = error.type.element?.name;
todo = '''
Loading