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

[Core] Avoid NPEs on creating events #179

Merged
merged 2 commits into from
May 23, 2024
Merged

[Core] Avoid NPEs on creating events #179

merged 2 commits into from
May 23, 2024

Conversation

saraadams
Copy link
Collaborator

@saraadams saraadams commented May 23, 2024

When creating trace event format events, the code used to throw NullPointerExceptions when trying to construct an event from a JsonObject that was missing required members.

This change adds static fromJson methods, which check the members exist, or otherwise throw an IllegalArgumentException. This better communicates the kind of error that occurred - the provided Json did not have the expected form.
This change also adds some basic documentation and tests for the Event classes.

When creating trace event format events, the code used to throw
NullPointerExceptions when trying to construct an event from a
JsonObject that was missing required members.

This change adds static `fromJson` methods, which check the members exist,
or otherwise throw an IllegalArgumentException.
It also adds some basic tests for the Event classes.

Signed-off-by: Sara Adams <sara.e.adams@gmail.com>
Copy link

@laszlocsomor laszlocsomor left a comment

Choose a reason for hiding this comment

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

Could you please explain in the PR message why throwing IAE is better than NPE?

Comment on lines +45 to +54
List<String> missingMembers = Lists.newArrayList();
for (String requiredMember : REQUIRED_JSON_MEMBERS) {
if (!event.has(requiredMember)) {
missingMembers.add(requiredMember);
}
}
if (!missingMembers.isEmpty()) {
throw new IllegalArgumentException(
"Missing members: " + Arrays.toString(missingMembers.toArray()));
}

Choose a reason for hiding this comment

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

In a follow-up, this could become a generic helper method.

@@ -0,0 +1,26 @@
/*
* Copyright 2022 EngFlow Inc.

Choose a reason for hiding this comment

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

2024?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Signed-off-by: Sara Adams <sara.e.adams@gmail.com>
@saraadams
Copy link
Collaborator Author

Could you please explain in the PR message why throwing IAE is better than NPE?

Done. Although throwing NPE vs IAE can also be seen as a matter of preference.

@saraadams saraadams merged commit d1fbd68 into main May 23, 2024
3 checks passed
@saraadams saraadams deleted the sara-avoid-NPE branch May 23, 2024 13:29
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.

3 participants