Skip to content

Commit

Permalink
Improve error message when environment map contains null values (#3671)…
Browse files Browse the repository at this point in the history
… (#3672)

Co-authored-by: Oliver Marienfeld <schwaigbub@gmail.com>
  • Loading branch information
oliver-brm and Oliver Marienfeld committed May 17, 2022
1 parent f7c4bb8 commit 61e08e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

/** Immutable configuration options for the container. */
Expand Down Expand Up @@ -141,9 +142,14 @@ public Builder setEnvironment(@Nullable Map<String, String> environmentMap) {
Preconditions.checkArgument(
!Iterables.any(environmentMap.keySet(), Objects::isNull),
"environment map contains null keys");
String nullValuedKeys =
environmentMap.entrySet().stream()
.filter(entry -> entry.getValue() == null)
.map(Map.Entry::getKey)
.collect(Collectors.joining(", "));
Preconditions.checkArgument(
!Iterables.any(environmentMap.values(), Objects::isNull),
"environment map contains null values");
nullValuedKeys.isEmpty(),
"environment map contains null values for key(s): " + nullValuedKeys);
this.environmentMap = new HashMap<>(environmentMap);
}
return this;
Expand Down
Expand Up @@ -106,7 +106,7 @@ public void testBuilder_nullValues() {
ContainerConfiguration.builder().setEnvironment(nullValueMap);
Assert.fail();
} catch (IllegalArgumentException ex) {
Assert.assertEquals("environment map contains null values", ex.getMessage());
Assert.assertEquals("environment map contains null values for key(s): key", ex.getMessage());
}
}

Expand Down

0 comments on commit 61e08e1

Please sign in to comment.