-
Notifications
You must be signed in to change notification settings - Fork 10
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
Conversation
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>
There was a problem hiding this 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?
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())); | ||
} |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2024?
There was a problem hiding this comment.
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>
Done. Although throwing NPE vs IAE can also be seen as a matter of preference. |
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.