Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-608.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: ServiceException no longer throws NPE when null args are provided
links:
- https://github.com/palantir/conjure-java-runtime-api/pull/608
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,37 @@ public List<Arg<?>> getParameters() {
}

private static <T> List<T> copyToUnmodifiableList(T[] elements) {
if (elements == null || elements.length == 0) {
return Collections.emptyList();
}
List<T> list = new ArrayList<>(elements.length);
Collections.addAll(list, elements);
for (T item : elements) {
if (item != null) {
list.add(item);
}
}
return Collections.unmodifiableList(list);
}

private static String renderUnsafeMessage(ErrorType errorType, Arg<?>... args) {
String message = renderNoArgsMessage(errorType);

if (args.length == 0) {
if (args == null || args.length == 0) {
return message;
}

StringBuilder builder = new StringBuilder();
boolean first = true;
builder.append(message).append(": {");
for (int i = 0; i < args.length; i++) {
Arg<?> arg = args[i];
if (i > 0) {
builder.append(", ");
for (Arg<?> arg : args) {
if (arg != null) {
if (first) {
first = false;
} else {
builder.append(", ");
}
builder.append(arg.getName()).append("=").append(arg.getValue());
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to keep the information that an arg was null in the message? E.g. rendering something like {arg1=null}? That way you could see that something went wrong passing through the args when inspecting the logs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that tells us anything, and we would have to use a funky argument name to prevent duplication with user-provided arg names.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense!

}

builder.append(arg.getName()).append("=").append(arg.getValue());
}
builder.append("}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public void testExceptionMessageWithUnsafeArgs() {
assertThat(ex.getMessage()).isEqualTo(EXPECTED_ERROR_MSG + ": {arg1=1, arg2=2}");
}

@Test
public void testExceptionMessageWithNullArg() {
ServiceException ex = new ServiceException(ERROR, UnsafeArg.of("arg1", 1), null, SafeArg.of("arg2", 2));
assertThat(ex.getMessage()).isEqualTo(EXPECTED_ERROR_MSG + ": {arg1=1, arg2=2}");
assertThat(ex.getArgs()).doesNotContainNull().hasSize(2);
}

@Test
public void testExceptionMessageWithNoArgs() {
ServiceException ex = new ServiceException(ERROR);
Expand Down